单击带有Selenium Web驱动程序的Google链接 [英] Clicking on link through Google with Selenium web driver

查看:74
本文介绍了单击带有Selenium Web驱动程序的Google链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用硒网络驱动程序打开Goog​​le搜索Youtube,然后单击Youtube链接. (我知道我可以直接去那儿,但这是一个概念证明)我曾尝试过不同的解决方案,但是在查找Youtube链接供我单击时一直遇到问题.

I am trying to use selenium web driver to open up Google search for Youtube and then click on the Youtube link. (I know I could go straight there but this is a proof of concept) I have played around with different solutions, but I keep having problems locating the Youtube link for me to click on.

这就是我所拥有的:

driver = webdriver.Chrome()
driver.get("https://google.com")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("youtube")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
link = driver.find_element_by_link_text('YouTube')
link.click()

我无法识别HTML,因此只需在Google上对其进行检查即可查看YouTube的HTML.

I can't get the HTML to recognize so just inspect it on google to see the HTML for Youtube.

也欢迎使用python回答.

Also answers in python would be appreciated.

感谢您的帮助.

推荐答案

以下是您的问题的答案:

Here is the Answer to your Question:

To open up Google search for Youtube and then click on the Youtube link,下面是您自己的工作代码,并进行了一些简单的调整:

To open up Google search for Youtube and then click on the Youtube link, here is your own working code with some simple tweaks:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver=webdriver.Chrome("C:\\Utility\\BrowserDrivers\\chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(20)
driver.get("https://google.com")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("youtube")
elem.send_keys(Keys.RETURN)
time.sleep(3)
assert "No results found." not in driver.page_source
driver.find_element_by_xpath('.//*[@id="rso"]/div[1]/div/div/div/div/h3/a').click()

这篇关于单击带有Selenium Web驱动程序的Google链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆