[英]NoSuchElementException: Selenium unable to locate search bar regardless of which selector I use (xpath, css_selector, fullXpath etc..)
I'm trying to automate search procedure on facebook.我正在尝试在 facebook 上自动化搜索过程。 I used the following code我使用了以下代码
search = driver.find_element_by_xpath('//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input')
search.send_keys('foo') # search for 'foo'
submit = driver.find_element_by_css_selector('#jsc_c_11 > div > a > div > div.hpfvmrgz.g5gj957u.buofh1pr.rj1gh0hx.o8rfisnq > span > span')
submit.click()
Note that I had attempted the same procedure beforehand using请注意,我之前曾尝试使用相同的程序
WebDriverWait(f.browser, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input'))).send_keys("foo")
To no avail.无济于事。 I keep getting the same error我不断收到同样的错误
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="mount_0_0_ij"]/div/div[1]/div/div[2]/div[2]/div/div/div[1]/div/div/label/input"} NoSuchElementException:消息:没有这样的元素:无法定位元素:{"method":"xpath","selector":"///*[@id="mount_0_0_ij"]/div/div[1]/div/div[ 2]/div[2]/div/div/div[1]/div/div/label/input"}
with a TimeoutException for the last entry.最后一个条目有一个 TimeoutException 。
I have managed to automate login using selenium but these NoSuchElementExceptions
occur so frequently, I'm starting to wonder if there's a problem with my device我已经设法使用 selenium 自动登录,但这些NoSuchElementExceptions
发生得如此频繁,我开始怀疑我的设备是否有问题
Note : the following code works just fine for my coworkers注意:以下代码适用于我的同事
If it is the facebook Search Facebook
which is located on the left corner top side , you can use the belwo css_selector :
如果是位于左上角的 facebook Search Facebook
,则可以使用 belwo css_selector :
input[name='global_typeahead']
code :代码 :
wait = WebDriverWait(driver, 10)
search = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='global_typeahead']")))
search.send_keys('foo')
Imports :进口:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
PS :- Those css or xpath are not looking reliable, they may work or may not work, always try to write your own locators using axes. PS :- 那些 css 或 xpath 看起来不可靠,它们可能有效也可能无效,总是尝试使用轴编写自己的定位器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.