1. 程式人生 > >python webdriver 報錯WebDriverException: Message: can't access dead object的原因(pycharm中)

python webdriver 報錯WebDriverException: Message: can't access dead object的原因(pycharm中)

sage return tac class word cli esp self. 手機

PyCharm中運行firefox webdriver訪問郵箱添加通訊錄的時候報錯-WebDriverException: Message: can‘t access dead object

調了半天,發現是因為在登錄操作後沒有從frame中切換出來導致的,因為在登錄的時候需要先切換到frame中,登錄之後要切換出來才能繼續其他操作。

下面是我運行的代碼,driver.switch_to.default_content()這一行被我註釋掉了,結果就報這個錯


代碼:
driver=webdriver.Firefox(executable_path=‘c:\\geckodriver‘)
driver.get(‘http://mail.126.com‘)
try:
wait=WebDriverWait(driver,10,0.2)#顯示等待
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id=‘x-URS-iframe‘]"))#切換到用戶名和密碼輸入框所在的frame元素

name=wait.until(lambda x:x.find_element_by_xpath("//input[@placeholder=‘郵箱帳號或手機號‘ and @name=‘email‘]"))
name.send_keys(‘xiaxiaoxu1987‘)
password=wait.until(lambda x:x.find_element_by_xpath("//input[@placeholder=‘密碼‘]"))
password.send_keys(‘gloryroad‘)
submit=wait.until(lambda x:x.find_element_by_xpath("//a[@id=‘dologin‘]"))
submit.click()
#driver.switch_to.default_content()#在pycharm裏用switch_to_default_content()會被加刪除線,out了

address_book_link=wait.until(lambda x:x.find_element_by_xpath("//div[text()=‘通訊錄‘]"))
address_book_link.click()


運行報錯:

C:\Python27\python.exe D:/test/dataDrivenTestPractice1/PageObject/test.py
Traceback (most recent call last):
File "D:/test/dataDrivenTestPractice1/PageObject/test.py", line 27, in <module>
address_book_link=wait.until(lambda x:x.find_element_by_xpath("//div[text()=‘通訊錄‘]"))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
File "D:/test/dataDrivenTestPractice1/PageObject/test.py", line 27, in <lambda>
address_book_link=wait.until(lambda x:x.find_element_by_xpath("//div[text()=‘通訊錄‘]"))
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 387, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 957, in find_element
‘value‘: value})[‘value‘]
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 314, in execute
None
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
WebDriverException: Message: can‘t access dead object


Process finished with exit code 0

把註釋去掉:

driver=webdriver.Firefox(executable_path=‘c:\\geckodriver‘)
driver.get(‘http://mail.126.com‘)
try:
wait=WebDriverWait(driver,10,0.2)#顯示等待
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@id=‘x-URS-iframe‘]"))#切換到用戶名和密碼輸入框所在的frame元素

name=wait.until(lambda x:x.find_element_by_xpath("//input[@placeholder=‘郵箱帳號或手機號‘ and @name=‘email‘]"))
name.send_keys(‘xiaxiaoxu1987‘)
password=wait.until(lambda x:x.find_element_by_xpath("//input[@placeholder=‘密碼‘]"))
password.send_keys(‘gloryroad‘)
submit=wait.until(lambda x:x.find_element_by_xpath("//a[@id=‘dologin‘]"))
submit.click()
driver.switch_to.default_content()#在pycharm裏用switch_to_default_content()會被加刪除線,out了



address_book_link=wait.until(lambda x:x.find_element_by_xpath("//div[text()=‘通訊錄‘]"))
address_book_link.click()

結果:沒有報錯

C:\Python27\python.exe D:/test/dataDrivenTestPractice1/PageObject/test.py

Process finished with exit code 0

python webdriver 報錯WebDriverException: Message: can't access dead object的原因(pycharm中)