【发布时间】:2017-07-17 03:39:09
【问题描述】:
我正在尝试输入搜索词并从搜索中获取热门网址列表。我是 Selenium 的新手,不知道自己在做什么。到目前为止,我一直在关注这个教程:https://www.youtube.com/watch?v=EELySnTPeyw&t=21s
一切正常,直到返回热门网址。我使用的 xPath 是经过检查后直接来自谷歌上的元素。这是我当前的代码:
from selenium import webdriver
def get_results(search_term):
url = "https://www.google.com"
driver = webdriver.Chrome()
driver.get(url)
search_box = driver.find_element_by_id("lst-ib")
search_box.send_keys(search_term)
search_box.submit()
links = driver.find_element_by_xpath("//*[@id="rso"]/div[3]/div/div[1]/div/h3/a")
results = []
for link in links:
href = link.get_attribute("href")
print(href)
results.append(href)
driver.close()
return results
get_results("Who is the president of the united states?")
运行此程序时,我在 xPath ("//*[@id="rso"]/div[3]/div/div[1]/div/h3/a") 上不断收到无效语法错误。关于为什么这不起作用的任何想法?谢谢
【问题讨论】:
-
如果你想得到
list的元素,你应该使用find_elements_by_xpath()而不是find_element_by_xpath()
标签: python html google-chrome selenium xpath