Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium in Python, Unable to Click 'button': Tag not found

TL,DR:

For whatever reason, my selenium python script can't seem to "click" on the buttons I need.

Context:

Hello. My task is one many are probably familiar with: I'd like to automate the process of opening a website, logging in, and clicking on a few drop-down menu links within the website, which will ultimately lead me to a page where I can download a spreadsheet. I'm able to open the web page and log in. To proceed, I have to:

  1. Click on the drop down menu header, and
  2. in the drop down menu, click on the appropriate option.

Here's a snapshot of the pertinent HTML code from the website:

<td class="x-toolbar-cell" id="ext-gen45">
    <table id="ext-comp-1045" class="x-btn x-btn-noicon" style="width: auto;" cellspacing="0">
        <tbody class="x-btn-small x-btn-icon-small-left">
            <tr>
                <td class="x-btn-ml"><i>&nbsp;</i></td>
                <td class="x-btn-mc">
                    <em class="x-btn-arrow" unselectable="on">
    <button type="button" id="ext-gen46" class=" x-btn-text">Reports</button>
</em>
                </td>
                <td class="x-btn-mr"><i>&nbsp;</i></td>
            </tr>
        </tbody>
    </table>
</td>

The item I need to "click" has a button tag, specifically:

<button type="button" id="ext-gen46" class=" x-btn-text">Reports</button>

To select it with selenium, I've tried the following:

reports_click_element = browser.find_element_by_id('ext-gen46').click()

and when that failed,

reports_element = browser.find_element_by_xpath("//button[contains(text(), 'Reports')]").click()

That one actually executed without an ExceptionMessage error, but I found out it was selecting other elements in the page that had "Reports" text, as opposed to the particular button I need.

When I've tried to zero in on the button I need clicked, the interpreter returned an error message indicating that the html attributes could not be found.

How can I proceed from here? (Should I be focusing on the unselectable="on" tag in the element right above the button I need clicked?)

Please let me know if I can add anything to the question. Thanks in advance.

Update: I have switched into an iframe that I believe the menu is a part of- but I still cannot select the button. So far, here is my Python code:

from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
import time

binary = FirefoxBinary('C:\Program Files (x86)\Mozilla Firefox\Firefox.exe')
browser = webdriver.Firefox(firefox_binary=binary)

browser.get("https://app.website.com")

login_entry(username, password) # this works fine; it's just a user-created function to login. Ignore.

time.sleep(10) # wait for website's markup to load
browser.switch_to.frame(browser.find_element_by_tag_name("iframe"))
time.sleep(10)

# This is the point where I'm trying to click on the "Reports" button
reports_element = browser.find_element_by_xpath("//*[contains(text(), 'Reports')]") #this refers to other elements
reports_element = browser.find_element_by_xpath("//button[contains(text(), 'Reports')][1]") #no luck here either
like image 794
daOnlyBG Avatar asked May 25 '26 18:05

daOnlyBG


1 Answers

A couple of cases which came to mind.

More than one element exists but not all are visible

elements = browser.find_elements_by_xpath("//button[contains(text(), 'Reports')]")
for element in elements:

    if element.is_displayed():
        print "element is visible"
        element.click()
    else:
        print("element is not visible")
        print(element)

The element exists, it would be visible but is out of screen.

from selenium.webdriver.common.action_chains import ActionChains
elements = browser.find_elements_by_xpath("//button[contains(text(), 'Reports')]")
for element in elements:  
    ActionChains(driver).move_to_element(element).perform()
    try:
        element.click()
    except:
        print("couldn't click on {}".format(element)) 

Can you also try to record your clicks and keyboard entries with Selenium IDE for Firefox? Then save it as a python script and post it as a comment here?

like image 74
Maximilian Peters Avatar answered May 28 '26 08:05

Maximilian Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!