程序从moto页面检索数据- Python/ Selenium

lf3rwulv  于 2022-12-13  发布在  Python
关注(0)|答案(2)|浏览(121)

我正在做一个关于投资组合报废的课程。我不能选择保时捷的944选项,因为我有相同的课程,不同车型的div。有人能告诉我如何做吗?非常感谢你的帮助。

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.options import Options
import tkinter as tk
import time, os

...

def get_driver(self):
    driver = webdriver.Chrome(ChromeDriverManager().install(), options=self.option)
    driver.get('https://www.olx.pl/d/motoryzacja/samochody/porsche/')
    driver.maximize_window()
    driver.find_element(By.ID, 'onetrust-accept-btn-handler').click()  # cookies
    driver.find_element(By.CLASS_NAME, 'css-mf5jvh').click()           # scroll list
    # driver.find_element(By.CSS_SELECTOR, 'css-1ukn2f9')   # wrong code

网址是:
https://www.olx.pl/d/motoryzacja/samochody/porsche/
I would like to mark myself option 944

kqlmhetl

kqlmhetl1#

您可以尝试以下操作:

# to handle cookies button
driver.find_element(By.CSS_SELECTOR, "#onetrust-accept-btn-handler").click()

driver.find_element(By.XPATH, ".//*[text()='Model']/parent::div//div[@class='css-1ee1qo5']").click()
time.sleep(2)
driver.execute_script("window.scrollBy(0, 200)")
models = driver.find_elements(By.XPATH, ".//*[@data-testid='flyout-content']//p")
print(len(models))
option_to_select = "944"   # you can change this to any other option available in that dropdown

for i in range(len(models)):
    model_name = driver.find_element(By.XPATH, "(.//*[@data-testid='flyout-content']//p)[" + str(i + 1) + "]").text
    if model_name == option_to_select:
        driver.find_element(By.XPATH, "(.//*[@data-testid='flyout-content']//p)[" + str(i + 1) + "]").click()
        break
jbose2ul

jbose2ul2#

在这种情况下,您可以用途:

driver.find_element(By.XPATH, '//*[@id="root"]/div[1]/div[2]/form/div[3]/div[1]/div/div[3]/div/div/div[2]/div/div[7]/label/input').click()

我一般会建议在可能的情况下使用XPATH。根据我的经验,它是最可靠的属性。

相关问题