【问题标题】:Selenium Chrome getting text not work on headless modeSelenium Chrome 获取文本在无头模式下不起作用
【发布时间】:2019-02-01 19:47:16
【问题描述】:

在使用 Chrome webdriver 在无头模式下运行 selenium 时,我遇到了一种奇怪的行为。到目前为止,我之前在无头模式下获取文本之前没有这个问题,它一直有效。

下面给出了可重现的例子:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
#options.add_argument('--headless')
#options.add_argument('--no-sandbox')

driver = webdriver.Chrome(chrome_options=options)

driver.get("https://www.zoom.com.br/ar-condicionado/todos")

wait = WebDriverWait(driver, 10)

stores = wait.until(
    EC.presence_of_all_elements_located((By.XPATH,
                                        './/span[@class="storeCount-txt"]')))

print(stores[0].text)

当我运行这段代码时,输​​出是:

> em 14 lojas

但是,当我在无头模式下运行它(删除#s)时,输出为空:

> ""

有什么想法吗?

【问题讨论】:

  • 您是否尝试过driver.set_window_size(1920,1080) 或您之前的解决方案?这将确保元素出现在同一个地方。
  • @Rocky Li 没用……还是空的。它在无头模式下对你有用吗?
  • @RockyLi 我用 Firefox webdriver 进行了测试,它工作正常。也许这是 Chrome 网络驱动程序的一个错误。

标签: python selenium selenium-chromedriver


【解决方案1】:

试试这个。这应该可以工作。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument("--start-maximized")

driver = webdriver.Chrome(chrome_options=options,executable_path='D:/Java/TestChrome/lib/chromedriver.exe')

driver.get("https://www.zoom.com.br/ar-condicionado/todos")
wait = WebDriverWait(driver, 20)

stores = wait.until(EC.presence_of_all_elements_located((By.XPATH,'//span[@class="storeCount-txt"]')))
print("test : " + stores[0].get_attribute('innerHTML'))

让我知道这是否可行。

【讨论】:

  • 我想 OP 最好知道为什么这应该起作用
  • 因为java脚本在这里调用。所以如果你想从中获取值。你应该取属性值而不是文本。我想你明白我的意思了。
【解决方案2】:

当我运行部署在 heroku 中以无头模式运行 chrome 的网络爬虫脚本时,我遇到了同样的问题。我通过将以下 chrome 选项添加到我的选项列表中解决了这个问题

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("--start-maximized")
chrome_options.add_argument("--window-size=1920x1080") #I added this

正如您问题中的评论所述,可能有两件事可能使某些元素不显示

  1. 您使用的分辨率不显示我通过添加该选项解决的那个元素(或类似的东西)
  2. 您正在搜索尚未加载的元素。我建议适当等待(您已经使用 stores 变量完成了),您也可以使用它来代替

     try:
        # Wait until 'what you specified' is visible
        WebDriverWait(driver, 60) \
            .until(expected_conditions.visibility_of_element_located((By.XPATH, './/span[@class="storeCount-txt"]')))
     except Exception as exp:
        print("Exception occured", exp)
        driver.quit()
    

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-10
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2023-01-22
    • 1970-01-01
    相关资源
    最近更新 更多