Hack Forums
[Python] [Selenium/python] Help running headless with Chrome profile - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Python] [Selenium/python] Help running headless with Chrome profile (/showthread.php?tid=6136242)



[Selenium/python] Help running headless with Chrome profile - jmartle - Jan 25, 2021

Code
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
options.add_argument("--headless")
options.add_argument("user-data-dir=C:\\Users\\sd\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1\\Default")

driver = webdriver.Chrome(options=options)

If i run it without headless, with my browser open, everything works good. the Chrome profile works and the selenium script runs well.

but if i try to go headless, i get error. pls help i am noob

Code
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//input[@type='file']"}
  (Session info: headless chrome=87.0.410.191)



RE: [Selenium/python] Help running headless with Chrome profile - ๖ۣۜNyanbot - Jan 25, 2021

Might be loading the page too fast. Try looking into this.

https://selenium-python.readthedocs.io/waits.html


RE: [Selenium/python] Help running headless with Chrome profile - Nomad.Null - Jan 26, 2021

Just like Nyan said, you need to wait for the page to load either with a delay (dirty) or you should wait for the specific element you want:

PHP Code

<?php 
wait 
= WebDriverWait(driver, 10)
element = wait.until(element_condition) 


The "element_condition" can be the element itself that you're looking for once it appears on the page, or you can use an "expected condition" from the "selenium.webdriver.support.expected_conditions". There are several you can use. I use this for a LinkedIn scraper, just as an example, and it works great.