当前位置: 首页 > 面试题库 >

selenium可与Chrome一起使用,但不能与无头Chrome一起使用

欧阳俊明
2023-03-14
问题内容

我已经使用Selenium和最初的PhantomJS开发了一些Python脚本。在走向自动下载时,我改用了(带头的)Firefox(运行了),然后选择了无头选项的Chrome,这样我就不会打开浏览器了。

我的第一个脚本访问一个页面和几个HTML元素,与无头Chrome完美搭配。

但是第二个 仅适用于带头的Chrome
。如果添加“无头”选项,它将不再起作用。当我尝试以无头模式打印HTML以查看为什么找不到所需的HTML元素时,我所拥有的只是:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

使用Chrome浏览器,我可以打印完整的HTML。这就是我启动无头Chrome的方式:

options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors") 
options.add_argument("headless") 
driver = webdriver.Chrome(chrome_options=options)

再次注意,这在我的另一个脚本中有效。唯一的区别是我需要登录才能访问该页面,但是即使那样,为什么它也可以与头部配合使用?通过填写表格,我的脚本可以自动登录。

Python:3.6.1,Chrome:60.0.3112.78(64位),Selenium:3.4.3

任何想法?谢谢。

编辑:这是代码的开头

url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")

html_source = driver.page_source
print(html_source)

blocStatus = WebDriverWait(driver, TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()

问题答案:

我有和您一样的经历,并通过使用xvfb和pyvirtualdisplay解决了这一问题。

我使用chromedrive = v2.3.1,chrome-browser = v60和Selenium = 3.4.3

在无头Chrome中,某些脚本似乎无法按预期工作。

请参阅https://gist.github.com/addyosmani/5336747中的
vpassapera评论。

像下面这样尝试,

from pyvirtualdisplay import Display

display = Display(visible=0, size=(800, 600))
display.start()

# Do Not use headless chrome option
# options.add_argument('headless')

url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")

html_source = driver.page_source
print(html_source)

blocStatus = WebDriverWait(driver,    TIMEOUT).until(EC.presence_of_element_located((By.ID, "username")))
inputElement = driver.find_element_by_id("username")
inputElement.send_keys('actualLogin')
inputElement = driver.find_element_by_id("passwd")
inputElement.send_keys('actualPassword')
inputElement.submit()

display.stop()

xvfb必须使用“ pyvortualdisplay”

$ sudo apt-get install -y xvfb


 类似资料:
  • 我是Python和Selenium的初学者,我不知道我的代码或环境中有什么错误。。。 下面是错误消息。。。 C:\Python27\python.exeD:/PythonPratice/test.pyTraceback(最近的最后一次调用):File"D:/PythonPratice/test.py",第9行,在driver.get('https://www.google.com.tw/')Fil

  • 我有一个烧瓶服务器运行在http://127.0.0.1:5000和一个vuejs前端运行http://localhost:8080我已经做了api,并用postman测试了它,一切都如预期的那样工作:( > 将请求发布到/登录- (将请求发送至/登录)- 烧瓶API代码: 登录。vue: 指数vue 当我使用邮递员登录时,我得到的响应为;当我使用邮递员获取url/索引时,我得到响应。数据但当我使

  • 问题内容: 我已经在端口8080(默认)下启动并测试了Tomcat。现在,我将连接器端口更改为80,并重新启动了Tomcat,在最小的Debian 6.0安装中没有任何显示。现在,这里的窍门在哪里? 问题答案: 转到/ etc / default / tomcat6并更改为

  • 我想使用的与chrome API,但我遇到了一个问题... 我尝试了以下操作,但chrome API无法将识别为函数,因此我尝试先将保存为变量。。。 但是当我尝试以下操作时,仍然是一个空字符串。我不明白为什么,因为被设置为。我怎样才能解决这个问题? 编辑 我把剧本叫做... 在剧本里我有以下内容... 其中函数只返回一个字符串。它是由我的捕获的,我通过打印到if语句内的控制台来验证它。 我注意到是

  • 我试图用DataJpaTest注释测试我的存储库,但出现了一些奇怪的情况。 当我使用经典的时,一切正常,我的测试成功了。但是当我使用下面的生成器时,我的测试失败了。 测试应该成功,但其他测试没有成功,因为没有抛出关于约束有效性的异常。 例如,这里有一个失败的断言: Java语言AssertionError:预期测试将抛出org的实例。springframework。道。DataIntegrityV

  • 我的application.properties文件中有以下属性。 当我使用注释在spring控制器中使用属性时,它给我的值为2003,但当我通过获得它的值时,我获得的值为 如何使用AbstractenVironment获得值2003?