2017-10-08 63 views
0

這裏是我需要輸入新密碼並重復它,然後點擊'創建'的窗口。 enter image description herePython的webdriver從主窗口切換到彈出窗口(不是Java警報)和登錄

我迄今爲止代碼:

 createLogin = wait.until(EC.presence_of_element_located((By.XPATH, '//*[@id="Item.MessageUniqueBody"]/div/div/div/div/div[2]/div[2]/a'))) 
     createLogin.click() 
     time.sleep(10) 
     try: 
      newPassword = self.driver.find_elements_by_xpath('//*[@id="editNewUser_newPassword"]') 
      newPassword1 = self.driver.find_elements_by_xpath('//*[@id="editNewUser_newPasswordRepeat"]') 
      newPasswordForm = self.driver.find_elements_by_xpath('//*[@id="editNewUserPasswordForm"]/table/tbody/tr[1]/td[1]') 
      self.driver.switch_to.active_element(newPasswordForm) 
      time.sleep(3) 
      newPassword.send_keys('123') 
      newPassword1.send_keys('123') 
      time.sleep(2) 

      # createLog = wait.until(
      #  EC.presence_of_element_located((By.XPATH, '//*[@id="editNewUserPassword_save"]'))) 
      # createLog.click() 
      # time.sleep(5) 
     except NoAlertPresentException as e: 
      time.sleep(2) 
     myAccount = wait.until(
      EC.presence_of_element_located((By.XPATH, '//*[@id="easMyAccount1"]'))) 
     myAccount.click() 
     time.sleep(5) 
+0

什麼是問題嗎? –

+0

問題是 - 我得到一個錯誤:self.driver.switch_to.active_element(newPasswordForm) TypeError:'dict'對象不可調用 – user2771350

+0

active_element不是一種方法,它是一個包含有關活動元素信息的字典 –

回答

1

這就是問題所在。

您使用find_elements_by_xpath而不是find_element_by_xpath VS 奇異

find_elements_by_xpath:它爲您提供了具有匹配標識符的Web元素列表。

find_element_by_xpath:它爲您提供了具有匹配標識符的第一個Web元素。

newPassword = self.driver.find_element_by_xpath('//*[@id="editNewUser_newPassword"]') 
    newPassword1 = self.driver.find_element_by_xpath('//*[@id="editNewUser_newPasswordRepeat"]') 
    newPasswordForm = self.driver.find_element_by_xpath('//*[@id="editNewUserPasswordForm"]/table/tbody/tr[1]/td[1]') 
0

@gauurang答案是正確的,但你必須使用find_element_by_xpath,也爲您的XPath提示你有id找到webelements所以它始終是更好地使用id超過xpath

而且你的XPath是正確

newPassword = self.driver.find_element_by_id('editNewUser_newPassword') 
    newPassword1 = self.driver.find_element_by_id('editNewUser_newPasswordRepeat') 
    newPasswordForm = self.driver.find_element_by_xpath('//*[@id="editNewUserPasswordForm"]/table/tbody/tr[1]/td[1]') 
相關問題