如何通过Python Selenium打印链接列表。

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

How to print list of links through Python Selenium

问题

以下是您要翻译的代码部分:

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.wait import WebDriverWait
  5. # 创建 Chrome webdriver 实例
  6. driver = webdriver.Chrome()
  7. driver.get("https://www.skelbimai.lt/")
  8. WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']")))
  9. driver.find_element(By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']").click()
  10. WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//button[@mode='primary']")))
  11. driver.find_element(By.XPATH, "//button[@mode='primary']").click()
  12. driver.find_element(By.XPATH, "//input[@id='search-query']").click()
  13. driver.find_element(By.XPATH, "//input[@id='search-query']").send_keys("nokia 3310")
  14. driver.find_element(By.XPATH, "//select[@id='search-place']").click()
  15. city_search = driver.find_element(By.XPATH, "//select[@id='search-place']")
  16. city_target = driver.find_element(By.XPATH, "//option[@value='19']")
  17. driver.execute_script("arguments[0].scrollIntoView();", city_target)
  18. driver.execute_script("window.scrollTo(0, 0);")
  19. city_target = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='19']")))
  20. city_target.click()
  21. driver.find_element(By.XPATH, "//button[@type='submit']").click()
  22. listing_results = driver.find_element(By.XPATH, "//a[@class='classified ']")
  23. for x in range(2):
  24. print(listing_results.get_attribute('href'))
  25. driver.close()

希望这能帮助您解决问题。如果您有其他问题,请随时提出。

英文:

New to Python and Selenium and trying to print all of the links found in a specific category. For some reason it is only taking the first listing and repeating it. Could someone help please?

The gist of the code is the webdriver navigating to the page I need. The important part is at the end where I try to loop and get all the links.

  1. from selenium import webdriver
  2. from selenium.webdriver.common.by import By
  3. from selenium.webdriver.support import expected_conditions as EC
  4. from selenium.webdriver.support.wait import WebDriverWait
  5. # create instance of Chrome webdriver
  6. driver = webdriver.Chrome()
  7. driver.get(
  8. "https://www.skelbimai.lt/")
  9. WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']")))
  10. driver.find_element(By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']").click()
  11. WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//button[@mode='primary']")))
  12. driver.find_element(By.XPATH, "//button[@mode='primary']").click()
  13. driver.find_element(By.XPATH, "//input[@id='search-query']").click()
  14. driver.find_element(By.XPATH, "//input[@id='search-query']").send_keys("nokia 3310")
  15. driver.find_element(By.XPATH, "//select[@id='search-place']").click()
  16. city_search = driver.find_element(By.XPATH, "//select[@id='search-place']")
  17. city_target = driver.find_element(By.XPATH, "//option[@value='19']")
  18. driver.execute_script("arguments[0].scrollIntoView();", city_target)
  19. driver.execute_script("window.scrollTo(0, 0);")
  20. city_target = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='19']")))
  21. city_target.click()
  22. driver.find_element(By.XPATH, "//button[@type='submit']").click()
  23. listing_results = driver.find_element(By.XPATH, "//a[@class='classified ']")
  24. for x in range(2):
  25. print(listing_results.get_attribute('href'))
  26. driver.close()

答案1

得分: 0

In your loop, nothing was changing per iteration. You want this instead:

  1. listing_results = driver.find_elements(By.XPATH, "//a[@class='classified ']")
  2. for listing_result in listing_results:
  3. print(listing_result.get_attribute('href'))

You can also use SeleniumBase to simplify your entire script to:

  1. from seleniumbase import BaseCase
  2. BaseCase.main(__name__, __file__)
  3. class MyTestClass(BaseCase):
  4. def test_base(self):
  5. self.open("https://www.skelbimai.lt/")
  6. self.click("//a[@href='/skelbimai/technika/mobilus-telefonai']")
  7. self.click("//button[@mode='primary']")
  8. self.type("//input[@id='search-query']", "nokia 3310")
  9. self.click("//select[@id='search-place']")
  10. self.click("//option[@value='19']")
  11. self.click("//button[@type='submit']")
  12. listing_results = self.find_elements("//a[@class='classified ']")
  13. for listing_result in listing_results:
  14. print(listing_result.get_attribute("href"))

(If you pip install seleniumbase and then run the above script with python or pytest.)

英文:

In your loop, nothing was changing per iteration. You want this instead:

  1. listing_results = driver.find_elements(By.XPATH, "//a[@class='classified ']")
  2. for listing_result in listing_results:
  3. print(listing_result.get_attribute('href'))

You can also use SeleniumBase to simplify your entire script to:

  1. from seleniumbase import BaseCase
  2. BaseCase.main(__name__, __file__)
  3. class MyTestClass(BaseCase):
  4. def test_base(self):
  5. self.open("https://www.skelbimai.lt/")
  6. self.click("//a[@href='/skelbimai/technika/mobilus-telefonai']")
  7. self.click("//button[@mode='primary']")
  8. self.type("//input[@id='search-query']", "nokia 3310")
  9. self.click("//select[@id='search-place']")
  10. self.click("//option[@value='19']")
  11. self.click("//button[@type='submit']")
  12. listing_results = self.find_elements("//a[@class='classified ']")
  13. for listing_result in listing_results:
  14. print(listing_result.get_attribute("href"))

(If you pip install seleniumbase and then run the above script with python or pytest.)

huangapple
  • 本文由 发表于 2023年4月13日 21:45:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76006190.html
匿名

发表评论

匿名网友

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

确定