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'
8
  • You use .send_keys() on an element that you find on the page - not on the driver itself... Commented Jan 20, 2019 at 1: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 Commented Jan 20, 2019 at 1: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. Commented Jan 20, 2019 at 1: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... Commented Jan 20, 2019 at 1:53
  • If you are willing to switch to Firefox, you can adjust how long Firefox will try to load a page as described here. Commented Jan 20, 2019 at 3:52

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.