python selenium xpath multiple button using javascript issues

Viewed 42

i'm currently facing issue with selenium getting the exact xpath on a javascript button and appreciate if someone could enlighten me or guide me.

As you can see from the image, the highlighted ones are the javascript button. what i'm planning to do is to use selenium to find buttons that are showing "bill" and click into it to clear the status.

testing

my current codes are

while True:
    try:
        driver.find_element_by_xpath("//*[@id='main']/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[12]/a[1]")
    except NoSuchElementException:
        driver.refresh()
        time.sleep(5)
    else:
        time.sleep(3)
        driver.find_element_by_xpath("//*[@id='main']/div/div/div[2]/div/div[2]/table/tbody/tr[1]/td[12]/a[1]").click()

What i want it to do is to search for the "bill" button and if is not found, keep refreshing the page until "bill" button appear, then click on it which will bring it to another page. Do take note that it can be more than 1 "bill" button appearing (still have not figure out how to code for that part as well but i would want it to click on the row that appear the "bill" button with the earlier timing first which will bring to another page and after clearing on that page which will forward back to this page and proceed with the 2nd "bill" button etc.)

what happen now with my code is that the xpath element is only on the first row of "bill" button which it also can be "check" button being on the first row at the same position. And like i mention earlier on there could be multiple "bill" button coming so i'm not sure how to solve for that.

this are the html i get from inspecting from chrome browser for the "bill" button.

<a href="javascript:;" onclick="showContent('billing','/info/bill/12');" class="btn btn-danger btn-sm">
                                    <span class="glyphicon glyphicon-card">
                                    </span>
                                    &nbsp;bill
                                </a>

Really really appreciate if someone can help me out on this. thanks

1 Answers

Looks like your XPATH has been copy pasted. That should have worked since every XPATH is unique but let me show another way for declaring XPATH.

Try declaring XPATH in this format //tag_name[@attr1="value1" and @attr2="value2" and...and @attrn="valuen"]. In your Example:

while True:
    try:
        driver.find_element_by_xpath("//a[@class='btn btn-danger btn-sm' and @href='javascript;']")
    except NoSuchElementException:
        driver.refresh()
        time.sleep(5)
    else:
        time.sleep(3)
        driver.find_element_by_xpath("//a[@class='btn btn-danger btn-sm' and @href='javascript;']").click()

You Can also validate whether your XPATH is unique or not by searching the XPATH in your console. Click Ctrl+F in console and copy paste your XPATH. If the number of results is 1, it's fine. Else, try adding more attributes to your XPATH.

Also check in the console whether the control of the button stays with a or wrapped in another tag. If it is wrapped within another tag, then you might have to change the XPATH.

Related