from selenium import webdriverfrom selenium.common.exceptions import TimeoutExceptionfrom selenium.webdriver.support.ui import webdriverwait # available since 2.4.0from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0query = raw_input("What do you wish to search on Wikipedia?\n")query = " " + query# Create a new instance of the firefox driverdriver = webdriver.firefox()# go to the Google home pagedriver.get("https://www.Google.com/search?q=site%3Awikipedia.com&IE=utf-8&oe=utf-8")# the page is AJAXy so the Title is originally this:print driver.Title# find the element that's name attribute is q (the Google search Box)inputElement = driver.find_element_by_name("q")# type in the searchinputElement.send_keys(query)# submit the form (although Google automatically searches Now without submitting)inputElement.submit()try: # we have to wait for the page to refresh,the last thing that seems to be updated is the Title # You should see "cheese! - Google Search" print driver.Title driver.find_element_by_xpath("//h3[contains(text(),'Wikipedia')]").click()finally: driver.quit()
我正在尝试使用Selenium文档中的示例,因此请原谅这些注释,有时还需要不必要的代码.
我遇到问题的代码行是:
driver.find_element_by_xpath("//h3[contains(text(),'Wikipedia')]").click()
我正在尝试做的是获取相关的维基百科链接,或者更具体地说,获取H3’r’路径所指向的链接.
Here’s a picture of a Google page that I’m describing.
在这种情况下,我希望拉出链接http://en.wikipedia.com/wiki/salary
对不起文字的墙,我想尽可能具体.不管怎样,谢谢您的帮助.
最好的祝福!
@R_301_6120@ 问题是这个XPath不正确 – 有一个元素在文本中有“Wikipedia”,而不是h3元素.修理它:driver.find_element_by_xpath("//a[contains(text(),'Wikipedia')]").click()
您甚至可以使用以下方法进一步简化它:
driver.find_element_by_partial_link_text("Wikipedia").click()总结
以上是内存溢出为你收集整理的如何使用Selenium,Python从Google搜索中提取链接全部内容,希望文章能够帮你解决如何使用Selenium,Python从Google搜索中提取链接所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)