执行搜索操作,而使用Chrome,但不能在Firefox(硒)

问题描述 投票:0回答:2

这是Chrome的代码片段:

driver.get("https://www.youtube.com/")
driver.find_element_by_xpath('//*[@id="search"]').send_keys("kao")
driver.find_element_by_xpath('//*[@id="search-icon-legacy"]').click()

浏览器会自动打开YouTube和搜索所传递的字符串[这是我打算做任务]

当我开始使用properly.I我基本上使用相同的代码,但我每次运行时firefox.The加载页面出现的主要问题,它引发以下错误:

Message: Element <g id="search"> is not reachable

我不能任何字符串传递到搜索栏,甚至点击它。

python-3.x selenium-webdriver selenium-firefoxdriver
2个回答
0
投票

有在它返回两个元件如以下屏幕的xpath //*[@id='search']微小误差。究其原因是在上面的XPath的*作为外卡和其他ytd-searchbox<id="search">标签页面中的匹配。这同样适用于//*[@id="search-icon-legacy"]如此。

所以,你必须尝试改变到的XPath像下面唯一找到该网页元素

//input[@id='search']
//button[@id='search-icon-legacy']

误码的XPath:

enter image description here


0
投票

您的XPath是不正确的。这是无法找到搜索栏和按钮

您可以使用此一个

driver.get("https://www.youtube.com/")
driver.find_element_by_xpath("//input[@id='search']").send_keys("kao")
driver.find_element_by_xpath("//button[@id='search-icon-legacy']").click()
© www.soinside.com 2019 - 2024. All rights reserved.