当前位置: 首页 > 知识库问答 >
问题:

硒和铬一起工作,但不是无头铬

海保臣
2023-03-14

我使用Selenium和PhantomJS开发了几个Python脚本。当我转向自动下载时,我切换到了(headed)Firefox(有效),然后是Chrome,有了headless选项,这样浏览器就不会在我面前打开了。

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

第二个,然而,工作与头铬。如果我加上“无头”选项,它就不再起作用了。当我试图以headless模式打印HTML以查看它为什么找不到我要查找的HTML元素时,我所拥有的是:

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>
options = webdriver.ChromeOptions()
options.add_argument("--ignore-certificate-errors") 
options.add_argument("headless") 
driver = webdriver.Chrome(chrome_options=options)

你知道吗?谢了。

**编辑:这是代码的开头**

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

共有1个答案

南宫云
2023-03-14

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

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

在Headless chrome中,一些脚本似乎不能像预期的那样工作。

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()
$ sudo apt-get install -y xvfb 
 类似资料:
  • 如何让下载以无头模式启动?

  • 我还是想不通。几天前我的硒工作得很好;现在它给我带来了一些错误。我首先使用Nuget,然后我尝试手动安装它。 如何重现问题: 错误: OpenQA. Selenium.WebDriverExcture:抛出一个带有空响应的异常,向远程WebDriver服务器发送HTTP请求以获取URLhttp://localhost:60695/session.异常的状态为ReceiveFailure,消息为:基

  • 我在我的Ruby on Rails(5.2.1)项目中使用chrome headless with Selenium(3.14.0)和Capybara(3.8.0),我有一个测试,它可以在无头chrome中工作,但不能在无头chrome中工作。我在谷歌chrome稳定版69上使用的是“--无头”标志。 我已经用下面的设置了我的无头chrome,这适用于所有不下载文件的测试。

  • headless Chrome可以通过MacOS上的Selenium Webdriver使用自签名证书吗? 我试图让Rails系统测试由SSL上的headless Chrome驱动。 测试设置 样品测试 页面内容: MacOS 10.13.6 Rails 5.2.1 Ruby 2.4.1 (gem)3.5.1 (gem)3.14.0 (gem)2.34 铬68.0.3440.106。也尝试过 谷歌

  • 问题内容: 我正在使用python-selenium和Chrome 59,并尝试自动执行简单的下载序列。当我正常启动浏览器时,下载有效,但是当我以无头模式运行时,下载不起作用。 我什至尝试添加默认路径: 在常规实现中,添加默认路径是可行的,但是无头版本仍然存在相同的问题。 如何使下载以无头模式开始? 问题答案: 是的,这是安全的“功能”。如前所述,这里是Bug讨论:https : //bugs.c

  • 我有一段硒代码来寻找某个元素,然后点击这个相同的元素。找到该元素,但在此之后,单击似乎没有解决问题。以下是用于查找元素的代码(不幸的是使用xpaths,因为没有id,并且它使用了很多自制方法): 我使用上面的字符串xpathButton将WebElement存储在变量中,然后将WebElements传递给另一个类中的方法:SearchForElementByXPathWithWithOutSwit