3

I am trying to save a screenshot of a webpage, to do so I am trying to use Selenium. The problem is that once the webpage is opened, it stays blank with "data:" in the URL.

Here is my code:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options,executable_path='aPath/chromedriver.exe',service_log_path='aPath/mylog.txt')
driver.get('http://myURL.html')
screenshot=driver.save_screenshot('aPath/my_screenshot.png')
driver.quit()

NB: I have checked that my chromedriver version is compatible with my chrome browser version.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
Nefarious62
  • 161
  • 1
  • 5
  • 15
  • It remains that way utter `driver.get('http://myURL.html')`? – Guy Jan 13 '20 at 13:38
  • Hi @Guy, I am not sure to understand your comment, but I can say that I have tried with IE and it works fine, the problem is that I really need to make it work on Chrome – Nefarious62 Jan 13 '20 at 14:00
  • You are navigating to a page using `driver.get()`. After this command is it still showing blank page? or only before it? – Guy Jan 13 '20 at 14:01
  • When I use debug mode, it never get to the line `driver.get('http://myURL.html')`, it stay indefinitely at the line `driver = webdriver.Chrome(options=options,e.....` – Nefarious62 Jan 13 '20 at 14:10
  • If it get stuck in the middle you should get an exception eventually, post it please. – Guy Jan 13 '20 at 14:13
  • It stays like 1 minute without any change then I get the following error: `selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist` – Nefarious62 Jan 13 '20 at 14:18
  • 1
    Try https://stackoverflow.com/questions/56637973/how-to-fix-selenium-devtoolsactiveport-file-doesnt-exist-exception-in-python – Guy Jan 13 '20 at 14:20
  • @Nefarious62 try removing all the options and only use `executable_path`. – Jortega Jan 13 '20 at 14:21
  • Are you trying to load / parse an HTML document through Selenium? There might be a better extension for this. – CEH Jan 13 '20 at 15:15

3 Answers3

3

Thanks for your help guys, actually Guy was right, I had to specify the port:

options.add_argument('--remote-debugging-port=9222')

Now it works!

Nefarious62
  • 161
  • 1
  • 5
  • 15
2

You need to update the value of the Key executable_path with the absolute path of the chromedriver binary and service_args as follows:

driver = webdriver.Chrome(options=options,executable_path=r'C:\path\to\chromedriver.exe', service_args=["--log-path=C:\\path\\to\\mylog.log"])

You can find a couple of relevant discussions in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

I found that adding the following option worked for me:

options.add_argument('--no-sandbox')

Details found in this answer: selenium.common.exceptions.WebDriverException: Message: unknown error: unable to discover open pages using ChromeDriver through Selenium