0

I have tried to use selenium with python to automate some task. The program will navegate the link javascript:LoadPage('12') by click on it. However, I couldn't do it with selenium. My codes are below, any suggestion please kindly help.

webdriver.find_element_by_partial_link_text('Security')
webdriver.find_element_by_link_text("javascript:LoadPage('12')")
webdriver.find_element_by_link_text('Security')

Three of them are the codes that I tried but it doesn't work. I got an error something liMessage: no such element: Unable to locate element: {"method":"partial link text","selector":"Security"}

Thank you in advance.

Edit1: Add html code

<table height="40px" cellspacing="0" cellpadding="0" border="0">
    <tbody>
        <tr>
            <td width="90px" height="40px" bgcolor="#d8d8d8">
                <a href="javascript:LoadPage('0')" target="" class="Menu_L1_Link">
                    <p align="center">Status</p>
                </a>
            </td>
            <td width="90px" height="40px" bgcolor="#d8d8d8">
                <a href="javascript:LoadPage('6')" target="" class="Menu_L1_Link">
                    <p align="center">Network</p>
                </a>
            </td>
            <td width="90px" height="40px" bgcolor="#FFC000">
                <a href="javascript:LoadPage('12')" target="" class="Menu_L1_Active">
                    <p align="center">Security</p>
                </a>
            </td>
            <td width="90px" height="40px" bgcolor="#d8d8d8">
                <a href="javascript:LoadPage('19')" target="" class="Menu_L1_Link">
                    <p align="center">Application</p>
                </a>
            </td>
            <td width="90px" height="40px" bgcolor="#d8d8d8">
                <a href="javascript:LoadPage('26')" target="" class="Menu_L1_Link">
                    <p align="center">Management</p>
                </a>
            </td>
        </tr>
    </tbody>
</table>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Sasiwut Chaiyadecha
  • 183
  • 1
  • 1
  • 10

2 Answers2

0

Try using the following:

hrefText= "javascript:LoadPage('12')"
driver.find_element_by_xpath("//a[@href='" + hrefText + "']")

the find_element_by_xpath can be done as one line, but splitting into two lines makes it simpler with managing " and ' within the strings

This assumes that the webelement is unique based on the html provided. If there are fruther errors or please share a link to your site and steps on what you're trying to achieve. I'm happy to support.

[Quick update - another method now i can copy the html code] [update - wrapping it in a webdriverwait] Using this xpath will allow you to select by text

#driver.find_element_by_xpath('//a[p[text()="Security"]]')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//a[p[text()="Security"]]')))

This is the a that contains the p that has the text Security (and is case sensitive).

For webdriverwait, You'll need the following imports:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

Highlights as so: devtools


[update 2 - JS execution]

If you want to inject JS into the browser you can try this:

driver.execute_script("javascript:LoadPage('12')")

I can't try this as i'm not on your router, but give it a go and let us know.

RichEdwards
  • 3,423
  • 2
  • 6
  • 22
  • it doesn't work, getting error `Unable to locate element: {"method":"xpath","selector":"//a[p[text()="Security"]]"}` – Sasiwut Chaiyadecha Aug 04 '20 at 09:31
  • It works against the html you shared (hence the screenshot) :-) - do you need to do any actions to make the menu visible? - or if you're struggling can you share a link to the site? – RichEdwards Aug 04 '20 at 09:33
  • if this is a script based site,are you aware of how to use webdriverwait? (again - happy to help with this as you need) – RichEdwards Aug 04 '20 at 09:33
  • It is not a pubilc link sir, I'm trying to automate some task with moy router. I just want to click on the element 'Security' which navegate to the javascript link. – Sasiwut Chaiyadecha Aug 04 '20 at 09:35
  • no worries :-) - so i've updated with a wedriver wait. Can you try that - and can you confirm if there are any other actions you need to do to get the link to appear on the screen? – RichEdwards Aug 04 '20 at 09:41
  • I think some misunderstanding here. I don't want to make "Security" appear on the screen but I want to click on the word "Security" on the screen. Then, it should navegate me to the link `javascript:LoadPage('12')` – Sasiwut Chaiyadecha Aug 04 '20 at 09:45
  • You can't click on the link the make the browser navigate without it being present on screen. i've added a new bit at the bottom to inject JS into your browser, see if that helps – RichEdwards Aug 04 '20 at 09:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/219180/discussion-between-sasiwut-chaiyadecha-and-richedwards). – Sasiwut Chaiyadecha Aug 04 '20 at 10:06
0

The desired element is a JavaScript enabled element, so to click on the element with text as Security 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, "td>a[class^='Menu_L1'][href*='12']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//td/a[starts-with(@class, 'Menu_L1_')]/p[text()='Security']"))).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
    

References

You can find a couple of relevant discussions on NoSuchElementException in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352