简体   繁体   English

Python Selenium,找个按钮

[英]Python Selenium, find a button

I have searched stackoverflow and tried everything, but nothing seems to work.我搜索了 stackoverflow 并尝试了所有方法,但似乎没有任何效果。 I am using Python3.8 with Selenium 3.141.0.我正在使用 Python3.8 和 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.我没有收到另一条错误消息,while 循环没有中断。

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?另外 - 你确定chromedriver.exe是正确的路径吗? Maybe better provide the absolute path.也许最好提供绝对路径。

The button is commented out (although # is no html comment).该按钮已被注释掉(尽管#不是 html 注释)。

# <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.加载一个 URL 并从页面返回文本。

PS: Have a look at this tutorial PS:看看这个教程

http://jonathansoma.com/lede/foundations-2018/classes/selenium/selenium-windows-install/ 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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