How to Click a link with a keyword

Viewed 142

I am learning selenium(with Python) and I was trying to make it like I would search for something on google and then save all the links that contain the word "Watches"(or any other thing that I am searching) but I don't think selenium offers that feature

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time

PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)

product = "watch"

driver.get("https://www.google.com/")

try:
    search = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.NAME,"q"))
    )
    search.clear()
    search.send_keys(product)
    search.send_keys(Keys.ENTER)
    watches = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.LINK_TEXT,"Watches"))
    )
    print(watches.text)
except:
    driver.quit()
    print("failed to load")

time.sleep(2)
driver.quit()

this is the code I wrote can somebody find a way to do it? Thanks

1 Answers

If you see for any keyword (Watch in this example), they can be present like :

  • Watches
  • Watch
  • watches
  • watch

Basically xpath is case sensitive, so you have to put or condition like this . Below xpath represent all of them..

//h3[contains(text(),'Watches') or contains(text(),'Watch') or contains(text(), 'watches') or contains(text(),'watch')]/..

Code :

driver = webdriver.Chrome(driver_path)
driver.maximize_window()
driver.get("https://www.google.com/")
wait = WebDriverWait(driver, 50)
product = "watch"
try:
    search = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME,"q")))
    search.clear()
    search.send_keys(product)
    search.send_keys(Keys.ENTER)
    watches = WebDriverWait(driver, 10).until(EC.presence_of_all_elements_located((By.XPATH,"//h3[contains(text(),'Watches') or contains(text(),'Watch') or contains(text(), 'watches') or contains(text(),'watch')]/..")))
    for prod in watches:
      print(prod.get_attribute('href'))
except:
    driver.quit()
    print("failed to load")

output :

https://www.myntra.com/watches
https://www.flipkart.com/watches/pr?sid=r18
https://www.titan.co.in/shop/watches
None
https://www.amazon.in/watch/s?k=watch
https://www.fastrack.in/shop/watches
https://www.helioswatchstore.com/watches
https://www.apple.com/in/watch/
https://www.shopclues.com/men-watches.html
https://www.shoppersstop.com/men-watches/c-A1080

Update 1 :

 watches = driver.find_elements(By.XPATH,"//h3[contains(text(),'Watches') or contains(text(),'Watch') or contains(text(), 'watches') or contains(text(),'watch')]/..")
    for prod in watches:
      print(prod.get_attribute('href'))
Related