我需要检查是否存在带有搜索建议的窗口。当您在搜索中输入内容时,会出现建议搜索的列表。我需要检查此弹出窗口是否存在。
代码试用:
import time
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
class YandexSearchRu(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Chrome()
def test_search(self):
driver = self.driver
driver.get("http://www.yandex.ru")
try:
input = driver.find_element_by_xpath("//*[@id='text']")
except NoSuchElementException:
driver.close()
input.send_keys("Тензор")
input.send_keys(Keys.RETURN)
time.sleep(5)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main()
答案 0 :(得分:0)
尝试执行此操作:
driver.get("http://www.yandex.ru")
try:
input = driver.find_element_by_xpath("//*[@id='text']")
input.send_keys("adfadf")
popup = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "body > div. i-bem.popup > div.popup__content")))
if popup.is_displayed():
print("popup disyplayed")
else:
print("popup not visible")
except NoSuchElementException:
答案 1 :(得分:0)
该元素不是弹出窗口,而是自动建议,并且要提取自动建议,可以使用以下解决方案:< / p>
代码块:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get('http://www.yandex.ru')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input__control.input__input"))).send_keys("Тензор")
print([auto_suggestion.text for auto_suggestion in WebDriverWait(driver, 5).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.popup__content>div.suggest2__content.suggest2__content_theme_normal li>span.suggest2-item__text")))])
控制台输出:
['тензор', 'тензор официальный сайт', 'тензор техподдержка', 'тензорное исчисление', 'тензор спб', 'тензорные ядра', 'тензорный анализ', 'тензор эцп', 'тензорезистор', 'тензор инерции']