【问题标题】:Unable to locate element when using python selenium webdriver chrome on Linux Ubuntu [duplicate]在 Linux Ubuntu 上使用 python selenium webdriver chrome 时无法找到元素 [重复]
【发布时间】:2020-05-28 21:34:32
【问题描述】:

使用 python,我试图获取 url 上“Advances -”文本前面的数字: https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market

我正在使用下面的 xpath:

//*[@id="livepreOpenAdv"]

但出现以下错误:

没有这样的元素:无法找到元素:{"method":"xpath","selector":"//*[@id="livepreOpenAdv"]"} (会话信息:headless chrome=83.0.4103.61)

我在 Linux (ubuntu 20.04) 中使用 chromedriver

下面是python代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()

options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

driver.implicitly_wait(60)
driver.get(strURL)

print("Finding element.")

try:
    # advances = driver.find_element_by_id("livepreOpenAdv")
    advances = driver.find_element_by_xpath('''//*[@id="livepreOpenAdv"]''').text
    print(advances)
except Exception as ex:
    print(ex)

driver.close()

print("closed driver")

此代码在 Windows 中运行良好。无法弄清楚这里有什么问题。

【问题讨论】:

  • 您需要使用WebDriverWait。我有时能得到39,有时我不明白。这个起点可能会有所帮助。

标签: python selenium-webdriver xpath


【解决方案1】:

添加以下导入:

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

并将以advances = driver... 开头的行替换为:

advances = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[@id='livepreOpenAdv']"))).getText()

【讨论】:

  • 获取超时异常错误:回溯(最近一次调用最后一次):文件“t.py”,第 33 行,在 中前进 = WebDriverWait(driver,60).until(EC.visibility_of_element_located( (By.XPATH, "//span[@id='livepreOpenAdv']"))) 文件 "/home/ubuntu/.local/lib/python3.8/site-packages/selenium/webdriver/support/wait.py ",第 80 行,直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
  • 删除disable-gpu 选项(Linux 系统不需要)并将no-sandbox 作为第一个参数传递。还要从脚本中删除 driver.implicitly_wait(60)。另外,请务必检查您的 Chrome 驱动程序版本和 Chrome 浏览器是否兼容。
  • 我的linux系统中没有安装chrome浏览器。我安装了 chrome,并安装了 chromedriver。现在两者都是相同的版本,但仍然面临相同的问题。
  • 我尝试打印 driver.title,但它给了我“拒绝访问”。试图找到解决方法,但还没有运气。其他网址如 www.blgger.com 工作正常。
  • 那你应该检查一下:stackoverflow.com/a/54433282/9978746
【解决方案2】:

最后,下面的代码对我有用。我必须通过 apt 安装 google-chrome-stable 包。 @E.Wiest 的建议也有帮助。谢谢! 由于这是无头执行,因此 url 给出了拒绝访问消息。添加了用户代理参数来解决这个问题。

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time

strURL="https://www.nseindia.com/market-data/pre-open-market-cm-and-emerge-market"

options = Options()
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36')

driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver",options=options)

t1=time.time()
driver.get(strURL)

strXpath_advances='//*[@id="livepreOpenAdv"]'
strXpath_decline='//*[@id="livepreOpenDec"]'

try:
    advances = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_advances)))
    decline = WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH, strXpath_decline)))
    print("Advances={} Decline={}".format(advances.text,decline.text))
except Exception as ex:
    print("Exception=",ex)

t2=time.time()-t1
driver.close()
print("Done. closed driver. Time={0:.2f}".format(t2))

【讨论】:

    猜你喜欢
    • 2016-08-31
    • 2021-04-12
    • 1970-01-01
    • 2022-09-30
    • 2014-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多