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

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中)

原文地址:https://www.cnblogs.com/xiaxiaoxu/p/9290969.html

时间: 2024-08-08 00:08:37

python webdriver 报错WebDriverException: Message: can't access dead object的原因(pycharm中)的相关文章

python+appium 【已解决】真机运行appium报错“WebDriverException: Message: A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c.......详见内文

问题报错提示: selenium.common.exceptions.WebDriverException: Message: A new session could not be created. (Original error: Command failed: C:\Windows\system32\cmd.exe /s /c "D:\AutoTest\adt-bundle-windows-x86-20140702\sdk\platform-tools\adb.exe -s GWY0216A

thinkphp5访问报错 ...with message &#39;mkdir(): Permission denied&#39; in... 或...failed to open stream: Permission denied&#39; in...

报错截图1: 报错截图2: 报错原因没有权限访问thinkphp5/runtime目录. 解决方法: 1.如果thinkphp5/runtime目录下面有文件,先清空thinkphp5/runtime目录: 2.修改thinkphp5/runtime目录权限为可读可写. thinkphp5访问报错 ...with message 'mkdir(): Permission denied' in... 或...failed to open stream: Permission denied' in.

Python递归报错:RuntimeError: maximum recursion depth exceeded in comparison

Python中默认的最大递归深度是989,当尝试递归第990时便出现递归深度超限的错误: RuntimeError: maximum recursion depth exceeded in comparison 简单方法是使用阶乘重现: 1 #! /usr/bin/env Python 2 3 def factorial(n): 4 5 if n == 0 or n == 1: 6 7 return 1 8 9 else: 10 11 return(n * factorial(n - 1)) >

python 编码报错问题 &#39;ascii&#39; codec can&#39;t encode characters 解决方法

python在安装时,默认的编码是ascii, 当程序中出现非ascii编码时,python的处理常常会报这样的错 'ascii' codec can't encode characters python没办法处理非ascii编码的, 此时需要自己设置将python的默认编码,一般设置为utf8的编码格式. 查看python的默认编码 print sys.getdefaultencoding() 解决方法一(已通过验证,顺带也解决了我之前字符前一直加u的问题) 在python安装目录下,进入\P

安装python模块报错 error: command &#39;gcc&#39; failed with exit status 1

最近在安装paramiko模块的时候,总是报错:error: command 'gcc' failed with exit status 1,一开始比较挠头.找了蛮多资料,说的大多都是说缺少Python-devel 包,然而并不是! 最后蛮费劲的找到了一遍短小但就是正确的博文:http://blog.csdn.net/fenglifeng1987/article/details/38057193 —————————————————————— 解决方法: 安装:yum install gcc li

运行python代码报错UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte 0xe7 in position 91: ordinal not in range(128)的解决办法

1.通过搜集网上的资料,自己多次尝试,问题算是解决了,在代码中加上如下几句即可: import sys reload(sys) sys.setdefaultencoding('utf-8') 2.原因就是Python的str默认是ascii编码,和unicode编码冲突,混淆了python2 里边的 str 和 unicode 数据类型. 3.python3 区分了 unicode str 和 byte arrary,并且默认编码不再是 ascii. 运行python代码报错UnicodeDec

python执行报错 configparser.NoSectionError: No section: &#39;section_1&#39;

场景:请求获取验证码模块regVC.py读取配置文件config.ini时,regVC.py模块单独执行正常,但通过run_all.py模块批量执行时报错,找不到section 解决办法:配置文件路径需写绝对路径 config.ini文件如下: regVC.py模块代码如下: 1 import requests 2 import configparser 3 import unittest 4 from Case.readexcel import ExcelData 5 import json

appium---from appium import webdriver报错提示“Unresolved import webdriver”

报错提示: from appium import webdriver提示Unresolved import webdriver 报错原因:没有安装Appium_Python_Client 解决办法: 终端执行: 第一种:pip install Appium_Python_Client 第二种:git clone https://github.com/appium/python-client.git 原文地址:https://www.cnblogs.com/syw20170419/p/890072

python执行报错“UnicodeDecodeError: &#39;gbk&#39; codec can&#39;t decode byte 0xa1 in position 110: illegal multibyte sequence”

执行如下程序报错 import pytest @pytest.fixture() def user(): print("获取用户名") a = "zt" return a def test_1(user): assert user == "zt" if __name__ == "__main__": pytest.main(["-s","test_fixture1.py"]) Testi