繁体   English   中英

Python + 硒。 CSS 选择器或 Xpath

[英]Python + Selenium. CSS Selector or Xpath

如何在https://yandex.ru/的提示表中找到 CSS 选择器?

搜索带下划线的 CSS 不起作用( .mini-suggest__popup.mini-suggest__popup_svg_yes.mini-suggest__popup_theme_flat.mini-suggest__popup_visible

    browser.implicitly_wait(5)
try:
    suggest = browser.find_element_by_css_selector('.mini-suggest__popup.mini-suggest__popup_svg_yes.mini-suggest__popup_theme_flat.mini-suggest__popup_visible`')
except NoSuchElementException: 
print('no table with hints')

在此处输入图片说明

我会使用 CSS 选择器div.mini-suggest__popup 这是完整的示例:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM