简体   繁体   English

如何使用for循环点击selenium中具有相同XPATH的多个元素?

[英]How to click through multiple elements with same XPATH in selenium using for loop?

I'm trying to web scrape different elements with same class name.我正在尝试 web 抓取具有相同 class 名称的不同元素。 The following statements works well.以下语句运行良好。

browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[1]").click()
browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[2]").click()

... and so on. ... 等等。

Now, if I put this in a loop, it doesn't work.现在,如果我把它放在一个循环中,它就不起作用了。 Looks like, it doesn't recognize (//div[@class= 'jumbo-tracker'])[i]看起来,它不识别 (//div[@class= 'jumbo-tracker'])[i]

Here's the code:这是代码:

for i in range(1,length+1):
    browser.find_element(By.XPATH, "(//div[@class= 'jumbo-tracker'])[i]").click()
    sleep(5)
    browser.find_element(By.XPATH, "//div[@class='sc-1y3q50z-3 eiMLBn']").click()
    sleep(5)
    sno.append(restaurant)
    restaurant_name= browser.find_element(By.XPATH,"//h1[contains(@class, 'sc-7kepeu-0 sc-kafWEX kTxZkY')]").text
    name.append(restaurant_name)
    browser.back()
    browser.back()
    sleep(5)

Here's the exception:这是例外:

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"(//div[@class= 'jumbo-tracker'])[i]"}

Please, help.请帮忙。

You can click all elements keep in for loop and you have to use elements instead of element.您可以单击所有元素保留在 for 循环中,并且必须使用元素而不是元素。

clicks = browser.find_elements(By.XPATH, "//div[@class= 'jumbo-tracker']")

for click in clicks:
   click = click.click()
   time.sleep(2)

Use string interpolation.使用字符串插值。 String interpolation is a process substituting values of variables into placeholders in a string.字符串插值是将变量值替换为字符串中的占位符的过程。 This example (based on your code) uses an f-string:此示例(基于您的代码)使用 f 字符串:

for i in range(1,length+1):
    browser.find_element(By.XPATH, f"(//div[@class= 'jumbo-tracker'])[{i}]").click()

range()范围()

The range() function creates a sequence of numbers. range() function 创建一个数字序列。

So while passing the variable within the xpath using f-strings you need to convert the number as string for variable substitution and you can use the following line of code:因此,在使用f 字符串xpath中传递变量时,您需要将number转换为string以进行变量替换,您可以使用以下代码行:

for i in range(1,length+1):
        browser.find_element(By.XPATH, f"(//div[@class= 'jumbo-tracker'])[{str(i)}]").click()

暂无
暂无

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

相关问题 如何使用相同的 xpath 单击 Python Selenium 中的多个项目? - How can I click multiple items in Python Selenium with the same xpath? Python selenium 多次单击并返回相同 xpath - Python selenium multiple click and back with same xpath 在Python中使用Selenium单击具有相同类名的所有元素 - Using Selenium in Python to click through all elements with the same class name Scrapy&Selenium:如何循环XPATH并执行单击 - Scrapy & Selenium: How To Loop XPATH and preform a click 使用硒在一个for循环中遍历多个元素 - Iterating through multiple elements in one for loop using selenium 如何循环查找Selenium xpath中的子元素? - How to loop to find child elements in Selenium xpath? 如何选择相同的 xpath 级别以在 Selenium 中单击 - How to select same xpath level to click in Selenium 使用 Selenium 使用 get_elements_by_xpath 在循环中单击每个按钮后获取页面源 - Get page source after each button click in a loop with get_elements_by_xpath using Selenium 使用 selenium find_elements_by_xpath 多次返回相同元素的数组,而不是所有元素 - Using selenium find_elements_by_xpath is returning an array of the same element multiple times rather than all elements 即使使用 Selenium 和 Python 有多个具有相同类名的元素,如何通过类名识别元素 - How to identify an element through classname even though there are multiple elements with the same classnames using Selenium and Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM