Python + Selenium. CSS Selector or Xpath

I would use CSS selector div.mini-suggest__popup. Here is complete example:

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

chrome_options = webdriver.ChromeOptions()
#chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')

wd = webdriver.Chrome('<<PATH_TO_CHROMEDRIVER>>', options=chrome_options)

url = 'https://yandex.ru'

# load page via selenium
wd.get(url)

# wait 5 seconds until page will be loaded
text_input = WebDriverWait(wd, 5).until(EC.presence_of_element_located((By.ID, 'text')))
text_input.send_keys('Hello world')

# wait for popup to appear
popup = WebDriverWait(wd, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'div.mini-suggest__popup')))

# wait for popup third child and click it
WebDriverWait(popup, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'li:nth-child(3)'))).click();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top