Selenium的find_element在for循环中出现问题。

huangapple go评论70阅读模式
英文:

Selenium find_element broke for loop

问题

我使用find_elements找到元素,然后使用find_element获取子元素,但在find_element之后迭代无法继续进行,循环停止(尽管使用find_elements找到了多个元素)

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

def getItems(url, xpath):
    path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
    service = Service(executable_path=path_to_browser)
    browser = webdriver.Chrome(service=service)
    browser.get(url)
        
    # 等待页面成功加载
    WebDriverWait(browser, 10)

    return browser.find_elements(By.XPATH, xpath)

items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1", "/html/body/div[1]/div/main/div/div[2]/div/div[2]/div[2]/div[2]/div")
for idx, item in enumerate(items):
    # 仅打印索引为0的元素,然后停止
    print(idx)
    print(item.find_element(By.XPATH, ".//div/div[1]/div/div[1]/span[2]").text)
英文:

I find elements with find_elements and after getting child elements with find_element, but the iteration doesn't work further after find_element and the loop stops (although there are several elements found with find_elements)

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

def getItems(url, xpath):
   path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
   service = Service(executable_path=path_to_browser)
   browser = webdriver.Chrome(service=service)
   browser.get(url)
        
   # wait until page is succesfully loaded
   WebDriverWait(browser, 10)

   return browser.find_elements(By.XPATH, xpath)

items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1", "/html/body/div[1]/div/main/div/div[2]/div/div[2]/div[2]/div[2]/div")
for idx, item in enumerate(items):
    # only print element with index 0, and stops
    print(idx)
    print(item.find_element(By.XPATH, ".//div/div[1]/div/div[1]/span[2]").text)

答案1

得分: 2

这是您提供的代码的翻译:

因为这个定位器只返回一个元素,所以for循环只运行一次,我建议使用稳定的定位器而不是索引的XPaths。

请尝试以下代码:

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

def getItems(url, xpath):
    path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
    service = Service(executable_path=path_to_browser)
    browser = webdriver.Chrome(service=service)
    browser.get(url)

    # 等待页面成功加载
    WebDriverWait(browser, 10)

    return browser.find_elements(By.XPATH, xpath)

items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1",
                 '//div[contains(@class,"x-product-card__link")]')
for idx, item in enumerate(items):
    # 仅打印索引为0的元素,并停止
    print(idx)
    print(item.find_element(By.XPATH, ".//span[contains(@class,'x-product-card-description__price-new')]").text)

应该打印出所有60个结果。

英文:

Its because this locator return only a single element, so the for loop runs only once, Also i would suggest using stable locators instead of indexed xpaths

Try below

import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def getItems(url, xpath):
    path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
    service = Service(executable_path=path_to_browser)
    browser = webdriver.Chrome(service=service)
    browser.get(url)

    # wait until page is succesfully loaded
    WebDriverWait(browser, 10)

    return browser.find_elements(By.XPATH, xpath)


items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1",
                 '//div[contains(@class,"x-product-card__link")]')
for idx, item in enumerate(items):
    # only print element with index 0, and stops
    print(idx)
    print(item.find_element(By.XPATH, ".//span[contains(@class,'x-product-card-description__price-new')]").text)

Should print All 60

0
849 ₽
1
3 890 ₽
2
10 990 ₽
3
660 ₽
4
3 890 ₽
5
3 595 ₽
6
8 240 ₽
7
790 ₽
8
57 810 ₽
9
5 199 ₽
10
5 090 ₽
11
1 799 ₽
..

huangapple
  • 本文由 发表于 2023年5月17日 16:59:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定