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

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

Selenium find_element broke for loop

问题

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

  1. import os
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. def getItems(url, xpath):
  7. path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
  8. service = Service(executable_path=path_to_browser)
  9. browser = webdriver.Chrome(service=service)
  10. browser.get(url)
  11. # 等待页面成功加载
  12. WebDriverWait(browser, 10)
  13. return browser.find_elements(By.XPATH, xpath)
  14. 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")
  15. for idx, item in enumerate(items):
  16. # 仅打印索引为0的元素,然后停止
  17. print(idx)
  18. 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)

  1. import os
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. def getItems(url, xpath):
  7. path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
  8. service = Service(executable_path=path_to_browser)
  9. browser = webdriver.Chrome(service=service)
  10. browser.get(url)
  11. # wait until page is succesfully loaded
  12. WebDriverWait(browser, 10)
  13. return browser.find_elements(By.XPATH, xpath)
  14. 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")
  15. for idx, item in enumerate(items):
  16. # only print element with index 0, and stops
  17. print(idx)
  18. print(item.find_element(By.XPATH, ".//div/div[1]/div/div[1]/span[2]").text)

答案1

得分: 2

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

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

请尝试以下代码:

  1. import os
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. def getItems(url, xpath):
  7. path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
  8. service = Service(executable_path=path_to_browser)
  9. browser = webdriver.Chrome(service=service)
  10. browser.get(url)
  11. # 等待页面成功加载
  12. WebDriverWait(browser, 10)
  13. return browser.find_elements(By.XPATH, xpath)
  14. items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1",
  15. '//div[contains(@class,"x-product-card__link")]')
  16. for idx, item in enumerate(items):
  17. # 仅打印索引为0的元素,并停止
  18. print(idx)
  19. 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

  1. import os
  2. from selenium import webdriver
  3. from selenium.webdriver.chrome.service import Service
  4. from selenium.webdriver.common.by import By
  5. from selenium.webdriver.support.wait import WebDriverWait
  6. def getItems(url, xpath):
  7. path_to_browser = os.path.abspath(os.getcwd()) + "\\driver\\chromedriver"
  8. service = Service(executable_path=path_to_browser)
  9. browser = webdriver.Chrome(service=service)
  10. browser.get(url)
  11. # wait until page is succesfully loaded
  12. WebDriverWait(browser, 10)
  13. return browser.find_elements(By.XPATH, xpath)
  14. items = getItems("https://lamoda.ru/c/4153/default-women/?is_sale=1",
  15. '//div[contains(@class,"x-product-card__link")]')
  16. for idx, item in enumerate(items):
  17. # only print element with index 0, and stops
  18. print(idx)
  19. print(item.find_element(By.XPATH, ".//span[contains(@class,'x-product-card-description__price-new')]").text)

Should print All 60

  1. 0
  2. 849
  3. 1
  4. 3 890
  5. 2
  6. 10 990
  7. 3
  8. 660
  9. 4
  10. 3 890
  11. 5
  12. 3 595
  13. 6
  14. 8 240
  15. 7
  16. 790
  17. 8
  18. 57 810
  19. 9
  20. 5 199
  21. 10
  22. 5 090
  23. 11
  24. 1 799
  25. ..

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:

确定