selenium.common.exceptions.WebDriverException:消息:未知错误:元素...在点(x,y)处无法点击

时间:2018-01-01 16:34:17

标签: python-3.x selenium selenium-webdriver click

我正在尝试使用Selenium点击搜索框。 这是我的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time


chrome_options = Options()
chrome_options.add_argument("--disable-infobars")
chrome_options.add_argument("disable-infobars")
chrome_options.add_argument("--kiosk")

driver = webdriver.Chrome(executable_path="/mypath/chromedriver",
                              chrome_options=chrome_options)

thing_url = "https://www.target.com/"
driver.get(thing_url)
time.sleep(10)
searchbox = driver.find_element_by_id("search")
element_position = searchbox.location["y"]
driver.execute_script("window.scroll(0, {})".format(element_position))
time.sleep(10)
searchbox.click()

我做了很多谷歌搜索并应用了建议,例如在点击之前制作Chrome浏览器整页,或者在点击之前添加延迟等但没有一个工作。

以下是我收到的完整错误消息:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <input id="search" name="searchTerm" class="form--control js-searchTerm" type="search" autocorrect="off" autocapitalize="off" autocomplete="off" aria-labelledby="searchLabel"> is not clickable at point (955, 20). Other element would receive the click: <label id="searchLabel" for="search" data-text="search" aria-hidden="true">...</label>
  (Session info: chrome=63.0.3239.84)
  (Driver info: chromedriver=2.33.506106 (8a06c39c4582fbfbab6966dbb1c38a9173bfb1a2),platform=Mac OS X 10.12.6 x86_64)

我在Mac上使用Python 3.6。 在WebDriver打开Chrome后,我会看到如下页面:

enter image description here

请参阅搜索栏顶部的黑条,但几秒钟后黑条消失,看起来像搜索栏位置移动。请参见下图:

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试使用以下代码:

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


search_box = ui.WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "search")))
search_box.send_keys("Test.")

search_button = ui.WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#searchReset[data-search='submit']")))
search_button.click())

希望它可以帮到你!

答案 1 :(得分:0)

你使它变得比它需要的更复杂。当搜索框位于页面顶部时,您正在尝试滚动。当您需要做的只是INPUT时,您还试图点击.send_keys()

thing_url = "https://www.target.com/"
driver.get(thing_url) # no need for a sleep after this
driver.find_element_by_id("search").send_keys("shorts")

我假设您不为Target工作,因此您不需要测试UI。更简单的方法是使用搜索URL,例如

https://www.target.com/s?searchTerm=shorts

https://www.target.com/s?searchTerm=mens+shorts