0

I've written a simple function. The purpose of go_to_url so we can set a max load time for a page. If a page does not load within the timeout_limit, then we will try loading the page again. However, I am getting an error if the page does fail to load within the time frame and goes into the exception.

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException

def go_to_url(driver, url, timeout_limit):
    while True:
        try:
            driver.set_page_load_timeout(timeout_limit)
            driver.get(url)
            break;
        except TimeoutException:
            driver.send_keys(Keys.ESCAPE)
            print('Failed to load, reloading page')

options = webdriver.ChromeOptions()
options.add_argument("--incognito")
driver = webdriver.Chrome(options=options)

go_to_url(driver, "http://www.emba.com.tw/", 4)

Error:

Traceback (most recent call last):
  File "test.py", line 15, in go_to_url
    driver.get(url)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 333, in get
    self.execute(Command.GET, {'url': url})
  File "C:\Users\pc\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\pc\AppData\Local\Programs\Python\Python37-32\lib\site-packages
\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: timeout
  (Session info: chrome=71.0.3578.98)
  (Driver info: chromedriver=2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f743
87),platform=Windows NT 6.3.9600 x86_64)


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    go_to_url(driver, "http://www.emba.com.tw/", 4)
  File "test.py", line 18, in go_to_url
    driver.send_keys(Keys.ESCAPE)
AttributeError: 'WebDriver' object has no attribute 'send_keys'
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Yuri
  • 89
  • 1
  • 1
  • 9
  • You use `.send_keys()` on an element that you find on the page - not on the driver itself... – Jon Clements Jan 20 '19 at 01:30
  • @JonClements how do I set it on the page itself? I am essentially trying to stop the page from loading any further and try to reload it – Yuri Jan 20 '19 at 01:33
  • I don't use selenium that often but I'd imagine that'd be a command to the driver like `.cancel()`/`.stop()` or something... `.send_keys` is meant to send input to a focus'd element - not control the browser instance itself. – Jon Clements Jan 20 '19 at 01:52
  • Might want to make your question - "how do I stop a page page from continuing loading" or something like that in that case - as that's what your real question is - send_keys is a red herring here... – Jon Clements Jan 20 '19 at 01:53
  • If you are willing to switch to `Firefox`, you can adjust how long `Firefox` will try to load a page as described [here](https://stackoverflow.com/questions/21214340/make-selenium-webdriver-stop-loading-the-page-if-the-desired-element-is-already). – John Anderson Jan 20 '19 at 03:52
  • @JohnAnderson Unfortunately I'm too deep into the chrome code to switch now – Yuri Jan 20 '19 at 04:02
  • @Yuri instead of send_keys(), you can use javascript to stop loading the page, but the flip side of using a page_load_stop would be say your page starts loading by 3rd second and continues to load after 4th second, it will go into exception as you have given 4s as timeout. so the actual_page_load will stop and you need to go to loading part again. so unless you want to test the loading of the page with in given time, you don't want to repeat this action or u can increase the timeout of your page – SnR Jan 20 '19 at 04:21
  • I think when you get `TimeoutException` due to page load timeout, the driver itself will stop loading the page immediately, so ideally you don't have to do anything to stop the page loading. – Kamal Jan 21 '19 at 01:29

1 Answers1

0

Your code block was near perfect.

This error message...

AttributeError: 'WebDriver' object has no attribute 'send_keys'

...implies that the WebDriver implementation has no attribute as send_keys

selenium.webdriver.remote.webelement which reprsents a DOM element contains the method send_keys(*value) which defined as:

send_keys(*value)

 Simulates typing into the element.

So an appropiate way to invoke send_keys() must be associated on an element as follows:

driver.find_element_by_css_selector("input.string").send_keys("Yuri")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352