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

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

How to print list of links through Python Selenium

问题

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

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

# 创建 Chrome webdriver 实例
driver = webdriver.Chrome()
driver.get("https://www.skelbimai.lt/")
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']")))
driver.find_element(By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']").click()
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//button[@mode='primary']")))
driver.find_element(By.XPATH, "//button[@mode='primary']").click()
driver.find_element(By.XPATH, "//input[@id='search-query']").click()
driver.find_element(By.XPATH, "//input[@id='search-query']").send_keys("nokia 3310")
driver.find_element(By.XPATH, "//select[@id='search-place']").click()

city_search = driver.find_element(By.XPATH, "//select[@id='search-place']")
city_target = driver.find_element(By.XPATH, "//option[@value='19']")
driver.execute_script("arguments[0].scrollIntoView();", city_target)
driver.execute_script("window.scrollTo(0, 0);")
city_target = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='19']")))
city_target.click()
driver.find_element(By.XPATH, "//button[@type='submit']").click()

listing_results = driver.find_element(By.XPATH, "//a[@class='classified ']")

for x in range(2):
    print(listing_results.get_attribute('href'))

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.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait

    # create instance of Chrome webdriver
driver = webdriver.Chrome()
driver.get(
    "https://www.skelbimai.lt/")
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']")))
driver.find_element(By.XPATH, "//a[@href='/skelbimai/technika/mobilus-telefonai']").click()
WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, "//button[@mode='primary']")))
driver.find_element(By.XPATH, "//button[@mode='primary']").click()
driver.find_element(By.XPATH, "//input[@id='search-query']").click()
driver.find_element(By.XPATH, "//input[@id='search-query']").send_keys("nokia 3310")
driver.find_element(By.XPATH, "//select[@id='search-place']").click()

city_search = driver.find_element(By.XPATH, "//select[@id='search-place']")
city_target = driver.find_element(By.XPATH, "//option[@value='19']")
driver.execute_script("arguments[0].scrollIntoView();", city_target)
driver.execute_script("window.scrollTo(0, 0);")
city_target = WebDriverWait(driver, 1).until(EC.element_to_be_clickable((By.XPATH, "//option[@value='19']")))
city_target.click()
driver.find_element(By.XPATH, "//button[@type='submit']").click()

listing_results = driver.find_element(By.XPATH, "//a[@class='classified ']")

for x in range(2):
    print(listing_results.get_attribute('href'))

driver.close()

答案1

得分: 0

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

listing_results = driver.find_elements(By.XPATH, "//a[@class='classified ']")

for listing_result in listing_results:
    print(listing_result.get_attribute('href'))

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

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://www.skelbimai.lt/")
        self.click("//a[@href='/skelbimai/technika/mobilus-telefonai']")
        self.click("//button[@mode='primary']")
        self.type("//input[@id='search-query']", "nokia 3310")
        self.click("//select[@id='search-place']")
        self.click("//option[@value='19']")
        self.click("//button[@type='submit']")
        listing_results = self.find_elements("//a[@class='classified ']")
        for listing_result in listing_results:
            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:

listing_results = driver.find_elements(By.XPATH, "//a[@class='classified ']")

for listing_result in listing_results:
    print(listing_result.get_attribute('href'))

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

from seleniumbase import BaseCase
BaseCase.main(__name__, __file__)

class MyTestClass(BaseCase):
    def test_base(self):
        self.open("https://www.skelbimai.lt/")
        self.click("//a[@href='/skelbimai/technika/mobilus-telefonai']")
        self.click("//button[@mode='primary']")
        self.type("//input[@id='search-query']", "nokia 3310")
        self.click("//select[@id='search-place']")
        self.click("//option[@value='19']")
        self.click("//button[@type='submit']")
        listing_results = self.find_elements("//a[@class='classified ']")
        for listing_result in listing_results:
            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:

确定