简体   繁体   中英

Python Selenium, find a button

I have searched stackoverflow and tried everything, but nothing seems to work. I am using Python3.8 with Selenium 3.141.0.

This is the button:

<button id="task-open-filters-button" class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic" data-original-title="Edit Filter">
<span class="sr-only">Show / hide filter</span>
</button>

What I tried so far:

# Because the page is so slow, I work with try/except to get the element. 
# This works fine for a simple link, but not for this button
while True:
    try:
        # elem = browser.find_element_by_xpath('//button[@id="task-open-filters-button"]')
        # elem = browser.find_element_by_xpath('//button[@class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic"]')
        # elem = browser.find_element_by_link_text("Show / hide filter")
        # elem = browser.find_element_by_xpath("//*[@id='task-open-filters-button']")[0]
        # elem = browser.find_element_by_css_selector('.btn.btn-icon.icon-filter.list-filter-button.sn-tooltip-basic')
        elem = browser.find_element_by_id("task-open-filters-button")
        break
    except NoSuchElementException:
        print("Waiting for page to load!")
        sleep(1)
elem.click()

I do not get another error message, the while loop just does not break.

Do you guys have any idea what else to try? Thank you for your help!

When I look at your code, I see several potential issues.

There is a missing quote:

browser = webdriver.Chrome(chromedriver.exe")

Also - are you sure chromedriver.exe is the correct path? Maybe better provide the absolute path.

The button is commented out (although # is no html comment).

# <button id="task-open-filters-button"
# class="btn btn-icon icon-filter list-filter-button sn-tooltip-basic"
# data-original-title="Edit Filter">
# <span class="sr-only">Show / hide filter</span></button>

This definitely is correct syntax:

elem = browser.find_element_by_id("task-open-filters-button")

So I suggest make sure your setup is correct. Do something simple. Load an URL and return the text from the page.

PS: Have a look at this tutorial

http://jonathansoma.com/lede/foundations-2018/classes/selenium/selenium-windows-install/

It looks like the configuration is more like this

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.nytimes.com")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM