简体   繁体   English

如何使用 Selenium 单击 Python 中的扩展器按钮?

[英]How do you click an expander button in Python with Selenium?

I am working on a script for selenium to open a webpage, and click a series of buttons.我正在为 selenium 编写脚本以打开网页,然后单击一系列按钮。 The first button is an Expander button, shown in the below image第一个按钮是 Expander 按钮,如下图所示

展开按钮

The HTML code from inspecting the button is below, but the button itself is "class="dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc"检查按钮的 HTML 代码如下,但按钮本身是“class="dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc"

<div class="dojoExpandoPaneInner">
        <div data-dojo-attach-point="titleWrapper" class="dojoxExpandoTitle">
            <div class="dojoxExpandoContainer">
                <div class="dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc" data-dojo-attach-point="iconNode" data-dojo-attach-event="ondijitclick:_onIconNodeClick" tabindex="0" aria-controls="slideout"><span class="a11yNode">X</span></div>
            </div>

The xpath as well, is as follows: xpath 如下:

/html[@class='dj_webkit dj_chrome dj_contentbox has-webkit has-no-quirks svg']/body[@class='claro original']/div[@id='border']/div[@id='slideout']/div[@class='dojoExpandoPaneInner']/div[@class='dojoxExpandoTitle']/div[@class='dojoxExpandoContainer']/div[@class='dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc']

To find this button, I tried the following code one at a time.为了找到这个按钮,我一次尝试了以下代码。

driver.find_element_by_css_selector("div[class^='dojoxExpandoIcon']")    # find expander by class name
# driver.find_element_by_css_selector("div[class^='dojoxExpandoIcon dojoxExpandoIconLeft qa-button-toc']")    # find expander by class name
# driver.find_element_by_css_selector("div.dojoxExpandoContainer")

However, none of them have worked at all, and result in errors such as但是,它们都没有工作,并导致错误,例如

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc"}

Is there anything I'm doing wrong here?我在这里做错了什么吗?

Try waiting until the locator is clickable:尝试等到定位器可点击:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 15)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc")))
button = driver.find_element_by_css_selector(".dojoxExpandoIcon.dojoxExpandoIconLeft.qa-button-toc").click()

If the number of classes is not stable, remove the ones that are changing and leave only stable ones, for example:如果类的数量不稳定,则删除正在变化的类,只保留稳定的类,例如:

.dojoxExpandoIconLeft.qa-button-toc

Here I just removed one of classes.在这里,我刚刚删除了其中一个课程。

Read more about waits here https://selenium-python.readthedocs.io/waits.html .在此处阅读有关等待的更多信息https://selenium-python.readthedocs.io/waits.html

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

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