Python Selenium: Cant find element by xpath when browser is headless

2024/10/3 8:19:27

I'm attempting to log into a website using Python Selenium using the following code:

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

It works perfectly, however when I add the --headless option to the browser, it raises a NoSuchElementException.

Error raising code:

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:

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 runfileexecfile(filename, namespace)File "C:\Users\Alec\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfileexec(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_xpathreturn 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 executeself.error_handler.check_response(response)File "C:\Users\Alec\selenium\webdriver\remote\errorhandler.py", line 194, in check_responseraise 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)

This error only occurs when the browser is headless. What's causing this behavior? Can it be made to work in headless mode?

target 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>
Answer

I had the same issue. The solution was to add usr-agent:

user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'
chrome_options.add_argument('user-agent={0}'.format(user_agent))

I found the reason by printing the page

browser.get(url)
print(browser.page_source)

then the return html says permission deny, so I google it. Follow the solution: How to access a site via a headless driver without being denied permission

https://en.xdnf.cn/q/70753.html

Related Q&A

Reading large file in Spark issue - python

I have spark installed in local, with python, and when running the following code:data=sc.textFile(C:\\Users\\xxxx\\Desktop\\train.csv) data.first()I get the following error:---------------------------…

pyinstaller: 2 instances of my cherrypy app exe get executed

I have a cherrypy app that Ive made an exe with pyinstaller. now when I run the exe it loads itself twice into memory. Watching the taskmanager shows the first instance load into about 1k, then a seco…

python - Dataframes with RangeIndex vs.Int64Index - Why?

EDIT: I have just found a line in my code that changes my df from a RangeIndex to a numeric Int64Index. How and why does this happen?Before this line all my df are type RangeIndex. After this line of …

Uniform Circular LBP face recognition implementation

I am trying to implement a basic face recognition system using Uniform Circular LBP (8 Points in 1 unit radius neighborhood). I am taking an image, re-sizing it to 200 x 200 pixels and then splitting …

SQLAlchemy declarative one-to-many not defined error

Im trying to figure how to define a one-to-many relationship using SQLAlchemys declarative ORM, and trying to get the example to work, but Im getting an error that my sub-class cant be found (naturally…

Convert numpy.array object to PIL image object

I have been trying to convert a numpy array to PIL image using Image.fromarray but it shows the following error. Traceback (most recent call last): File "C:\Users\Shri1008 SauravDas\AppData\Loc…

Scheduling celery tasks with large ETA

I am currently experimenting with future tasks in celery using the ETA feature and a redis broker. One of the known issues with using a redis broker has to do with the visibility timeout:If a task isn’…

How to read out scroll wheel info from /dev/input/mice?

For a home robotics project I need to read out the raw mouse movement information. I partially succeeded in this by using the python script from this SO-answer. It basically reads out /dev/input/mice a…

Tell me why this does not end up with a timeout error (selenium 2 webdriver)?

from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWaitbrowser = webdriver.Firefox()browser.get("http://testsite.com")element = WebDriverWait(browser, 10).until…

PEP 8: comparison to True should be if cond is True: or if cond:

PyCharm is throwing a warning when I do np.where(temp == True)My full code:from numpy import where, arraya = array([[0.4682], [0.5318]]) b = array([[0.29828851, 0., 0.28676873, 0., 0., 0., 0., 0.288014…