简体   繁体   English

Python selenium - 在元素循环中查找元素

[英]Python selenium - Find element in elements loop

I would like to ask for help.我想寻求帮助。 I try on website https://www.kununu.com/de/volkswagen/kommentare/100 scrape overall rating under main title of all articles but when I do it, it will print:我在网站https://www.kununu.com/de/volkswagen/kommentare/100上尝试在所有文章的主标题下抓取总体评分,但是当我这样做时,它会打印:

4,8
4,8
4,8
4,8
4,8
4,8
4,8
4,8
4,8
4,8
4,8

But there are more ratings not just 4,8.但是还有更多的评分,而不仅仅是 4,8。 So I want to find element in elements loop.所以我想在元素循环中找到元素。 I would like to do it in this type of loop if it's possible.如果可能的话,我想在这种类型的循环中进行。 Here is my code:这是我的代码:

art = driver.find_elements_by_xpath("//article[@class='index__contentBlock__7vKo-']")
    for i in art:
        pr = i.find_element_by_xpath("//span[@class='index__score__16yy9']").text
        print(pr)

You've already gathered all the elements in art.你已经收集了艺术中的所有元素。

All you have to do is:您所要做的就是:

art = driver.find_elements_by_xpath("//article[@class='index__contentBlock__7vKo-']")
for i in art:
    print(i.text)

Let me know if that works.让我知道这是否有效。

This should print all articles with index_score.这应该打印所有带有 index_score 的文章。

art = driver.find_elements_by_xpath("//article[@class='index__contentBlock__7vKo-']//span[@class='index__score__16yy9']")

for i in art:
    print(i.text)

To extract the ratings eg 2,0 using Selenium and you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies :要使用Selenium提取评分(例如2,0) ,您必须为visibility_of_all_elements_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using CSS_SELECTOR and get_attribute("innerHTML") :使用CSS_SELECTORget_attribute("innerHTML")

     driver.get('https://www.kununu.com/de/volkswagen/kommentare/100') print([my_elem.get_attribute("innerHTML") for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div[class^='index__ratingBlock'] span[class^='index__score__']")))])
  • Using XPATH and text attribute:使用XPATHtext属性:

     driver.get('https://www.kununu.com/de/volkswagen/kommentare/100') print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[starts-with(@class, 'index__ratingBlock')]//span[starts-with(@class, 'index__score__')]")))])
  • Console Output:控制台输出:

     ['2,0', '4,5', '3,8', '4,8', '2,8', '4,7', '3,2', '4,0', '4,9', '4,2']
  • 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

Outro奥特罗

Link to useful documentation:链接到有用的文档:

暂无
暂无

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

相关问题 在 Python Selenium 上查找元素 - 缺少一个元素 - Find elements on Python Selenium - one element short python硒在循环中查找子元素 - python selenium find child elements in loop Selenium / Python:为什么在我的 for 循环迭代中找到第一个元素后,我的 Selenium find_element_by 不再找到元素? - Selenium / Python: Why isn't my Selenium find_element_by finding elements anymore after finding the first one in my for loop iterations? Python - Selenium:通过 find_elements_by() 循环抓取 AngularJS 元素 - Python - Selenium : Scraping AngularJS elements with loop over find_elements_by() Selenium和Webdriver遍历find_element_by_xpath()的元素列表 - Loop over list of elements for find_element_by_xpath() by Selenium and Webdriver Python Selenium find_element_by_xpath 找不到元素 - Python Selenium find_element_by_xpath can't find elements Selenium find_elements 在 Python 中一遍又一遍地返回相同的元素 - Selenium find_elements returns the same element over and over Python Python Selenium find_elements 没有给我当前元素 - Python Selenium find_elements not giving me the current element python selenium 通过 xpath 查找元素返回链接列表但元素不可交互 - python selenium find elements by xpath returns a list of a links but element not interactable python selenium按类查找元素返回整个网站而不是元素 - python selenium find elements by class return whole website instead of element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM