[英]How to find this element in selenium python
In the web page, there is many elements like this below and I want to find all of them and click one after one.在网页中,下面有很多这样的元素,我想找到所有这些元素并一个一个地点击。 they have the same name of class but different by ID.
它们具有相同的类名,但 ID 不同。
How I can find it and click?我怎样才能找到它并点击?
<a class="do do-task btn btn-sm btn-primary btn-block" href="javascript:;" data-task-id="1687466" data-service-type="3" data-do-class="do-task" data-getcomment-href="/tasks/getcomment/" data-check-count="0" data-max-check-count="2" data-money-text="0.08"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0.08 </font></font><i class="far fa-ruble-sign"></i></a>
To identify all of the similar elements and create a list you have to induce WebDriverWait for visibility_of_all_elements_located()
and you can use either of the following Locator Strategies :要识别所有相似的元素并创建一个列表,您必须为
visibility_of_all_elements_located()
引入WebDriverWait ,您可以使用以下任一定位器策略:
Using CSS_SELECTOR
:使用
CSS_SELECTOR
:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.do.do-task.btn.btn-sm.btn-primary.btn-block[data-task-id]")))
Using XPATH
:使用
XPATH
:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[@class='do do-task btn btn-sm btn-primary btn-block' and @data-task-id]")))
Next, you can access each list item and invoke click()
on each of them one by one.接下来,您可以访问每个列表项并逐个调用
click()
。
Note : You have to add the following imports :注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
If this css如果这个css
a.do.do-task.btn.btn-sm.btn-primary.btn-block
represent them, then you can use the below code :代表他们,然后你可以使用下面的代码:
for a in driver.find_elements_by_css_selector("a.do.do-task.btn.btn-sm.btn-primary.btn-block"):
a.click()
You may have to scroll to each element or put some delay before click, this depends on your use case and your automation strategy.您可能需要滚动到每个元素或在点击前放置一些延迟,这取决于您的用例和自动化策略。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.