I am doing a programme on portfolio - scrapping. I can't select the 944 option for Porsche because I have the same classes, div for different models. Could someone please show me how to do this? Thank you very much for your help.
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
the website is at the link:
https://www.olx.pl/d/motoryzacja/samochody/porsche/
I would like to mark myself option 944
CodePudding user response:
In this case you can use:
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()
I would generally advise to use XPATH when possible. In my experience it is the most reliable attribute.
CodePudding user response:
You can try this:
# 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