无法在 Python 上与 Selenium 中的元素进行交互

时间:2021-07-19 09:54:00

标签: python selenium

我一直在尝试搜索此网站“https://mol.org/regions/?regiontype=countries”。但我不断收到 ElementNotInteractableException。我在网上尝试了很多方法,但似乎没有任何效果。这是我的最新方法:

search_query = 'Egypt'
search_bar_XPath = '//div[@class="region-search"]/div/input[1]'

with webdriver as driver:
    wait = WebDriverWait(driver, 10)
    driver.get(url)
    wait.until(presence_of_element_located((By.CLASS_NAME, 'region-search')))
    search_bar = driver.find_element_by_xpath(search_bar_XPath)
    ActionChains(driver).move_to_element(search_bar).click().perform()
    wait.until(EC.element_to_be_clickable((By.XPATH, search_bar_XPath))).send_keys(search_query + Keys.RETURN)
    
    driver.close()

我不明白为什么搜索栏存在但无法交互或访问。

1 个答案:

答案 0 :(得分:1)

您的问题与浏览器屏幕尺寸有关:

试试这个:

driver.maximize_window()

我尝试了以下 code :

driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://mol.org/regions/?regiontype=countries")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Filter Political boundaries']"))).send_keys('Egypt')