【发布时间】: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