Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用python在Selenium中查找链接元素,该元素在其href中包含特定单词_Python 3.x_Selenium_Selenium Chromedriver_Webautomation_Chrome Web Driver - Fatal编程技术网

Python 3.x 使用python在Selenium中查找链接元素,该元素在其href中包含特定单词

Python 3.x 使用python在Selenium中查找链接元素,该元素在其href中包含特定单词,python-3.x,selenium,selenium-chromedriver,webautomation,chrome-web-driver,Python 3.x,Selenium,Selenium Chromedriver,Webautomation,Chrome Web Driver,我是Windows7用户,使用python 3.6.7和chromedriver 83.3 我喜欢用python实现自动化,最近开始用selenium和chromedriver实现web自动化。所以我对这个领域还很陌生 我写了一个脚本,在给它一个搜索查询后(花几个小时阅读教程和文档),可以从互联网上下载任何软件。这是我的剧本: 从selenium导入webdriver 导入时间 从selenium.webdriver.common.keys导入密钥 导入请求,bs4 查询=输入(“windows

我是Windows7用户,使用python 3.6.7chromedriver 83.3 我喜欢用python实现自动化,最近开始用selenium和chromedriver实现web自动化。所以我对这个领域还很陌生

我写了一个脚本,在给它一个搜索查询后(花几个小时阅读教程和文档),可以从互联网上下载任何软件。这是我的剧本:

从selenium导入webdriver
导入时间
从selenium.webdriver.common.keys导入密钥
导入请求,bs4
查询=输入(“windows软件的名称:”)
搜索谷歌=”https://www.google.com/search?q=“+”下载“+str(查询)+”适用于windows 7”
driver=webdriver.Chrome('chromedriver.exe')
链接=[]
website=requests.get(搜索谷歌)
website\u text=website.text
soup=bs4.BeautifulSoup(网站文字“lxml”)
所有链接=[]
查找汤中的链接。查找所有(“a”):
links.append(link.get(“href”))
对于链接中的链接:
如果链接中有“/url?q=”:
final=link.replace(“/url?q=“,”)
最终=最终分割(“&”,1)[0]
所有链接。附加(最终)
对于所有链接中的ss:
尝试:
驱动程序。获取(ss)
时间。睡眠(30)
下载=驱动程序。通过部分链接文本(“下载”)查找元素
下载。点击()
打印(下载.text)
退出
除:
#打印(下载.href)
打印(“未找到…移动到下一个…”)
持续
问题是,有时它会点击一些写着“下载”的链接,然后转到另一个页面,询问“开始下载”

我知道,当您下载exe文件时,要下载的链接包含以下内容:https://something.com/something/something.exe

所以我想问一下,如果它包含('.exe')的话,是否有一个find\u元素 或者:只点击其中包含“.exe”链接的任何内容

我是这个社区的新成员,如果您在我的问题中发现任何不符合预期的内容,我很抱歉。如果你在评论中问我,我会很高兴按照你的建议改变我的问题


顺便说一句,先谢谢你

您可以创建一个xpath或css表达式,将webelements与包含字符串“.exe”的
href
相匹配:

driver.find_element_by_xpath("//*[contains(@href,'.exe')]")
#or
driver.find_element_by_css_selector("[href*='.exe']")