4

I am trying to write a Python script to automate changing my Wi-Fi password from my router site (10.0.0.2). I'm using Selenium and I have been hitting a brick wall because I can't find some elements. Your help will highly be appreciated. I will provide all necessary resources for debugging below:

I succeeded logging into my router with find element by XPath.

Locates the submit/login button and clicks it:

browser.find_element_by_xpath('//input[@type="submit"]').send_keys(Keys.ENTER)`

Also, I tried several ways to find the element, but I commented them out temporarily.

#Locate Advanced Tab --1
browser.find_element_by_name("Advanced").click()

#Locate Advanced Tab --2
#tt1 = browser.find_element_by_xpath("//td[@id='topnav1']")
#tt1.click()

[1]#Locates the the advance tab on the page -- 3
#adv = browser.find_element_by_id('topnav1')
#adv.send_keys(Keys.RETURN)

This is the code for "Advanced" tab in my router:

<td id="topnav1" class="topnavoff" width="140"><a       href="javascript:ontopnav('1')">Advanced</a></td>

Edited: Code and error respectively.

browser.find_element_by_link_text("Advanced").click()

Traceback (most recent call last): File "celenium.py", line 22, in browser.find_element_by_link_text("Advanced").click() File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 337, in find_element_by_link_text return self.find_element(by=By.LINK_TEXT, value=link_text) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element 'value': value})['value'] File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute self.error_handler.check_response(response) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: Advanced


browser.find_element_by_css_selector("#topnav1>a").click()

Traceback (most recent call last): File "celenium.py", line 20, in browser.find_element_by_css_selector("#topnav1>a").click() File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 457, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element 'value': value})['value'] File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute self.error_handler.check_response(response) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: #topnav1>a


browser.find_element_by_css_selector("td.topnavoff#topnav1 > a ").click()

Traceback (most recent call last): File "celenium.py", line 22, in browser.find_element_by_css_selector("td.topnavoff#topnav1 > a ").click() File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 457, in find_element_by_css_selector return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 791, in find_element 'value': value})['value'] File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 256, in execute self.error_handler.check_response(response) File "C:\Users\Aesop\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: td.topnavoff#topnav1 > a


Snippet code from router site:

<table id=topnav_container2 cellspacing=0 border=0>
        <tbody>
        <tr>
          <td style="width: 1px; background-color: white"></td>
          <td id=modnum rowspan=2><img alt="" src="pic/DSL-G225.gif"></td>
          <td style="width: 2px; background-color: white"></td>
          <td id=topnav0 class=topnavon width=140 rowspan=2><a href="javascript:ontopnav('0')" >Setup</a></td>
          <td style="width: 2px; background-color: white"></td>
          <td id=topnav1 class=topnavoff width=140><a href="javascript:ontopnav('1')">Advanced</a></td>
          <td style="width: 2px; background-color: white"></td>
          <td id=topnav2 class=topnavoff width=140><a href="javascript:ontopnav('2')">Maintenance</a></td>
          <td style="width: 2px; background-color: white"></td>
          <td id=topnav3 class=topnavoff width=140><a href="javascript:ontopnav('3')">Status</a></td>
          <td style="width: 2px; background-color: white"></td>
          <td id=topnav4 class=topnavoff width=140><a href="javascript:ontopnav('4')">Help</a></td>
          <td style="width: 2px; background-color: white"></td>
        </tr>
        <tr>
          <td></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
          <td style="background-color: #404343"></td>
        </tr>
        </tbody>
      </table>
3
  • Do you have frames on your page? Commented Dec 27, 2017 at 11:36
  • @Ratmir Yes, sir. Commented Dec 27, 2017 at 11:39
  • @Originull when you say you have frames, did you switch to the exact frame? Post more of outerHTML containing the <frame> tag and your code trial to switch frames. Commented Dec 27, 2017 at 11:54

1 Answer 1

0

To click on the link with text as Advanced you can use any of the following line of code :

  • css_selector :

    browser.find_element_by_css_selector("table#topnav_container2 tr td.topnavoff#topnav1 > a").click()
    
  • xpath :

    browser.find_element_by_xpath("//table[@id='topnav_container2']//tr//td[@class='topnavoff' and @id='topnav1']/[contains(.,'Advanced')]").click()    
    

Note : The link with text as Advanced is within an <a> tag. Hence you must try to invoke the powerful click() method instead of send_keys(Keys.RETURN) or send_keys(Keys.ENTER) as follows :

browser.find_element_by_link_text("Advanced").click() 
Sign up to request clarification or add additional context in comments.

8 Comments

Thank you very much.... although I still have this bug. I don't think Advanced is a link per se. Which is why I think It's so difficult to locate it. :( I've tried your methods Ratmir and DebanjanB, and thank you once again, but I'm still failing.
@Originull Updated my Answer. Let me know the status. See the <a> tag have href attribute defined with value set to javascript:ontopnav('1'). So it is definitely a link. still failing doesn't throws any light on what is happening. Update the question with your latest code trials and error stack trace so the SO volunteers can help you out.
xpath and css looks fine as per the HTML. Can you show more of the outerHTML. Either there are multiple elements with same xpath/css or element is within a frame
I just pasted a snippet code. Would posting the whole page source code be better?
Thank you a lot for your time and help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.