How do I restart the page if Selenium does not find the element on the page?

Asked

Viewed 100 times

-2

from selenium import webdriver
from selenium.webdriver import Firefox
import time

url = 'https://google.com'
driver = webdriver.Firefox()
driver.get(url)
time.sleep(10)


driver.find_element_by_class_name('gLFy').send_keys('Rodrigo')
print('Ok')

I need you to refresh the page when you can’t find the element

2 answers

0

Try to change the find_element_by_class_name for find_element_by_xpath using like this:

driver.find_element_by_xpath("//input[@class='gLFyf gsfi']").send_keys('Rodrigo')
  • It is that I used only as an example this code, in case I am automating downloads there are times that the page does not load completely and have to give a refresh

  • and you already tried to use the driver.refresh() ?

0


from selenium import webdriver
from selenium.common import exceptions as ex

import time

url = 'https://google.com'
driver = webdriver.Firefox()

driver.get(url)
time.sleep(5)

try:
    driver.find_element_by_class_name('gLFy').send_keys('Rodrigo')
    print('Ok')
except ex.NoSuchElementException:
    driver.refresh()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.