3

I want to automate the restart of my router using Selenium on Python. Everything is working fine except for the last step, which is locating the restart button and click it!

I have tried to locate it by (id, css_selector, name, value, xpath), but nothing seemed to work.

Here is my code:

driver = webdriver.Firefox()
driver.get('http://192.168.100.1')

english = driver.find_element_by_id("English")
english.click()

usr = "username"
pwd = "password"

usrname_box = driver.find_element_by_id("txt_Username")
usrname_box.send_keys(usr)

pwd_box = driver.find_element_by_id("txt_Password")
pwd_box.send_keys(pwd)

submit_ = driver.find_element_by_id("button")
submit_.click()
sleep(1)


resetit = driver.find_element_by_name("maindiv_reset")
resetit.click()
sleep(1)

# This is the one I want to locate
reboot = driver.find_element_by_xpath("//input[@id='btnReboot']")
reboot.click()

And this is the HTML code for the target button:

<input class="ApplyButtoncss buttonwidth_150px" name="btnReboot" id="btnReboot" type="button" onclick="Reboot()" bindtext="s0603" value="Restart">

When trying anything, I get the error:

NoSuchElementException: Message: Unable to locate element: (WHATEVER I TRY)

A Screenshot of the HTML Page:

HTML Page

Thank you all for your help in advance.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
SoulSA
  • 83
  • 11

4 Answers4

1

The desired element is an JavaScript enabled element so to locate and click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.ApplyButtoncss.buttonwidth_150px#btnReboot[value='Restart']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='ApplyButtoncss buttonwidth_150px' and @id='btnReboot'][@value='Restart']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 1
    I suspect this is the right answer. Sounds like a race condition between selenuim and JavaScript. – Greg Burghardt May 30 '19 at 15:30
  • The code produced the error: `TimeoutException: Message: ` for both (CSS selector and XPath). It takes a moment before running into this error. – SoulSA May 30 '19 at 17:01
  • Can I excute a javascript code ( got it from the button javascript file ) using the `driver.execute_script(javascript)`, when I tried it, it gave me an error saying `JavascriptException: Message: SyntaxError: expected expression, got ')'` and I am not sure if what I am doing is right!? – SoulSA May 30 '19 at 18:25
  • @SoulSA Of coarse we do have the `execute_script()` option but that should be the last resort. However _TimeoutException_ is the outcome of **failed** _ExpectedConditions_. Can you debug your code through `find_element_by_*` in-conjunction with `time.sleep()` and if you are able to locate the element, update the question with the observations? Additionally, check if the element is within an ` – undetected Selenium May 30 '19 at 19:28
  • 1
    @DebanjanB I solved the problem thanks to your reply. Thank you. – SoulSA May 30 '19 at 20:06
0

Thy this one:

reboot = driver.find_element_by_name("btnReboot")
reboot.click()
Manuel
  • 117
  • 2
  • 9
  • Same issue: `NoSuchElementException: Message: Unable to locate element: [name="btnReboot"]` – SoulSA May 30 '19 at 14:17
  • try using: resetit = driver.find_element_by_class("ApplyButtoncss buttonwidth_150px") or resetit = driver.find_element_by_class_name("ApplyButtoncss buttonwidth_150px") – Manuel May 30 '19 at 14:22
  • It would be easier if the website was public and we can try the script by ourselves but try this one: driver.execute_script("Reboot()") – Manuel May 30 '19 at 15:07
  • I tried it, it gave me `JavascriptException: Message: ReferenceError: Reboot is not defined` – SoulSA May 30 '19 at 17:10
0

As the target button was included in iframe, I used the following method to solve the problem:

iframe = driver.find_element_by_id("frameContent")
driver.switch_to.frame(iframe)
driver.find_element_by_id('btnReboot').click()
alert = driver.switch_to_alert()
alert.accept()

Thanks a lot to everyone tried to help, especially to DebanjanB

SoulSA
  • 83
  • 11
-1

As a last ditch solution you can tell Python to click the Restart button assuming you can find the pixel (X, Y) coordinates on the screen and assuming the Firefox browser does not shift when running the script. See here.

Teuszie
  • 74
  • 1
  • 6