简体   繁体   English

如何使用 Python 高效迭代 Selenium Webdriver 中的表号?

[英]How to efficiently iterate table number in Selenium Webdriver using Python?

Please suggest me how to changes efficiently tr[1], [2], [3] or N numbers, following the table provided on the website automatically请建议我如何有效地更改 tr[1]、[2]、[3] 或 N 个数字,按照网站上提供的表格自动进行

code :代码 :

browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[1]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[2]/td[9]').click()
browser.find_element_by_xpath('//*[@id="event"]/tbody/tr[3]/td[9]').click()

You can consider using find_elements_by_xpath('.//tr') instead of find_element_by_xpath .您可以考虑使用find_elements_by_xpath('.//tr')而不是find_element_by_xpath

Alternatively, you can use a simple for loop with a try-except.或者,您可以使用带有 try-except 的简单 for 循环。 It's not the most elegant, but it will get you all N rows before going out due to the exception.它不是最优雅的,但由于异常,它会在出门前为您提供所有 N 行。

Try like below:尝试如下:

options = browser.find_elements_by_xpath('//*[@id="event"]/tbody/tr') # this xpath should highlight all the tr tags in the DOM

for opt in options:
    opt.find_element_by_xpath('./td[9]') # this should get the 9th td tag of that particular tr. Use a "." in the xpath to find element within an element.

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

相关问题 如何使用 Python 在 Selenium Webdriver 中高效迭代变量数? - How to efficiently iterate variable number in Selenium Webdriver using Python? 在Selenium 2 WebDriver(python)中遍历表 - Iterate through table in Selenium 2 WebDriver (python) 如何获取使用 Selenium WebDriver 和 Python 找到的元素数量? - How to get the number of elements found using Selenium WebDriver with Python? 如何使用python和Selenium Webdriver遍历表并打印前10行的结果? - How to iterate over a table and print the results from the first 10 rows with python and selenium webdriver? 我如何遍历表格并打印出列的值Selenium Webdriver Python - How do i iterate through a table and print out the values of the columns Selenium Webdriver Python 如何使用 Selenium WebDriver 和 Python 从表中获取元素(文本) - How to get element(text) from a table using Selenium WebDriver with Python 如何使用 Python 正确遍历 Selenium Webdriver 中的 Web 元素列表? - How do I properly iterate through a list of web elements in Selenium Webdriver using Python? python 使用 selenium webdriver - python using selenium webdriver 如何使用Python Selenium Webdriver显示价值? - How to display value using Python Selenium Webdriver? 如何在python中使用Selenium Webdriver滚动div - How to scroll a div using selenium webdriver in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM