Python Selenium:Can';当浏览器为headless时,无法通过xpath查找元素

Python Selenium:Can';当浏览器为headless时,无法通过xpath查找元素,python,selenium-webdriver,selenium-chromedriver,Python,Selenium Webdriver,Selenium Chromedriver,我正在尝试使用Python Selenium登录网站,使用以下代码: import time from contextlib import contextmanager from selenium import webdriver from selenium.webdriver.chrome.options import Options @contextmanager def getBrowser(*options): chrome_options = Options() if

我正在尝试使用Python Selenium登录网站,使用以下代码:

import time
from contextlib import contextmanager
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

@contextmanager
def getBrowser(*options):
    chrome_options = Options()
    if options: [chrome_options.add_argument(option) for option in options]
    browser = webdriver.Chrome(chrome_options=chrome_options)
    try:
        yield browser
    finally:
        browser.quit()


with getBrowser() as browser:
    browser.get('https://www.vinted.com/members/notifications')

    time.sleep(20)
    browser.find_element_by_xpath('//*[@id="content"]/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span').click()
但是,当我将
--headless
选项添加到浏览器中时,它会引发
NoTouchElementException

引发代码时出错:

with getBrowser('--headless') as browser:
    browser.get('https://www.vinted.com/members/notifications')

    time.sleep(20)
    browser.find_element_by_xpath('//*[@id="content"]/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span').click()
回溯:

Traceback (most recent call last):

  File "<ipython-input-4-fe0834deb137>", line 1, in <module>
    runfile('C:/Users/Alec/vinted test case.py', wdir='C:/Users/Alec')

  File "C:\Users\Alec\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\Users\Alec\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Alec/vinted test case.py", line 27, in <module>
    browser.find_element_by_xpath('//*[@id="content"]/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span').click()

  File "C:\Users\Alec\selenium\webdriver\remote\webdriver.py", line 354, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)

  File "C:\Users\Alec\selenium\webdriver\remote\webdriver.py", line 832, in find_element
    'value': value})['value']

  File "C:\Users\Alec\selenium\webdriver\remote\webdriver.py", line 297, in execute
    self.error_handler.check_response(response)

  File "C:\Users\Alec\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="content"]/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span"}
  (Session info: headless chrome=65.0.3325.181)
  (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 10.0.16299 x86_64)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('C:/Users/Alec/vinted test case.py',wdir='C:/Users/Alec')
文件“C:\Users\Alec\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第705行,在runfile中
execfile(文件名、命名空间)
文件“C:\Users\Alec\Anaconda3\lib\site packages\spyder\utils\site\sitecustomize.py”,第102行,在execfile中
exec(编译(f.read(),文件名,'exec'),命名空间)
文件“C:/Users/Alec/vinted test case.py”,第27行,在
浏览器。通过xpath('/*[@id=“content”]/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span')查找元素。单击()
文件“C:\Users\Alec\selenium\webdriver\remote\webdriver.py”,第354行,在find\u element\u by\u xpath中
返回self.find_元素(by=by.XPATH,value=XPATH)
文件“C:\Users\Alec\selenium\webdriver\remote\webdriver.py”,第832行,在find\u元素中
'value':value})['value']
文件“C:\Users\Alec\selenium\webdriver\remote\webdriver.py”,第297行,在execute中
self.error\u handler.check\u响应(响应)
文件“C:\Users\Alec\selenium\webdriver\remote\errorhandler.py”,第194行,在check\u响应中
引发异常类(消息、屏幕、堆栈跟踪)
NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id=“content”]/div/div/div[2]/div/div/div[6]/div[3]/div[3]/a/span”}
(会话信息:无头镀铬=65.0.3325.181)
(驱动程序信息:chromedriver=2.36.540470(e522d04694c7ebea4ba8821272dbef4f9b818c91),平台=Windows NT 10.0.16299 x8664)
此错误仅在浏览器无头时发生。是什么导致了这种行为?它能在无头模式下工作吗

目标HTML:

<div class="u-flex-grow">
      <a class="c-button--inverse c-button--normal c-button--amplified c-button " href="/member/general/login?ref_url=%2Fmembers%2Fnotifications"><span class="c-button__content">Log In</span></a>
    </div>

我曾经在使用这些工具的版本时遇到一些问题。我通过确保安装了最新版本的Chrome和webdriver修复了这个问题。也许是类似的问题

但除此之外,选择器取决于许多元素。因此,想象html在headless模式下看起来有点不同可能会导致您的问题

我会尝试获得一个不那么严格的选择器,如下面所示:
//a[以(@href,/member/general/login?)开头]


如果这不起作用,尝试将html从headless模式中转储到文件中。只是想看看无头浏览器看到了什么,并尝试用它构建一个奇特的选择器

我在python代码中添加了--headless时遇到了同样的问题。 我使用动作链解决了这个问题

from selenium.webdriver import ActionChains

text_list = browser.find_elements_by_xpath("//div[@class='tab-content']")
ActionChains(browser).click(text_list).perform()

尝试
浏览器。通过链接文本(“登录”)查找元素。单击()
相同问题。在headless中不起作用,但在其他方面起作用。你能用相关的HTML更新问题吗?@DebanjanB我已经包含了我试图与之交互的元素的HTML。当然,我提供的元素不会有相同的xpath