Search code examples
python-3.xseleniumselenium-chromedriverwebautomationchrome-web-driver

Finding a link Element in Selenium which contains a specific word in its href with python


I am windows 7 user using python 3.6.7 and chromedriver 83.3 I love automating stuff with python and recently started web automation with selenium and chromedriver. So i am pretty new to this field.

I wrote a script that can download any software from the internet after, (spending hours on tutorials and documentation reading) upon giving it a search query. Here is my script:

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
import requests, bs4

query = input("Name for a windows software: ")
searchGoogle = "https://www.google.com/search?q="+"download "+str(query)+" for windows 7"

driver = webdriver.Chrome('chromedriver.exe')

links = []
website = requests.get(searchGoogle)
website_text = website.text
soup = bs4.BeautifulSoup(website_text,"lxml")
all_links = []
for link in soup.find_all("a"):
    links.append(link.get("href"))
for link in links:
    if "/url?q=" in link:
        final = link.replace("/url?q=","")
        final = final.split("&", 1)[0]
        all_links.append(final)

for ss in all_links:
    try:
        driver.get(ss)
        time.sleep(30)
        download = driver.find_element_by_partial_link_text('Download')
        download.click()
        print(download.text)
        quit()
    except:
        #print(download.href)
        print("Not Found... Moving to next...")
        continue

the problem is that sometimes it clicks on some links that say "Download" and go to another page which asks "Start Download".

I know that when you download an exe file the link to download contains something like this: "https://something.com/something/something.exe"

So i wanted to ask if there was a find_element_if_its_href_contains('.exe') Or: anything that clicks only a link which contains ".exe" in it.

I am new to this community and I am sorry if you find anything in my question that does not meet StackOverflow Expectations. Ask me in the comments and i would be glad to change my question in the way that you suggest.

By The Way, Thanks in advance!


Solution

  • You can create an xpath or css expression to match webelements with href containing the string ".exe":

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