-1

How to get required item hrefs in a flipkart page?

This is my code:

from selenium import webdriver
driver = webdriver.Firefox(executable_path ="/home/yegaiah/Desktop/geckodriver") 
website_URL ="https://www.amazon.in/s?bbn=3403635031&rh=n%3A1984443031%2Cn%3A%211984444031%2Cn%3A3403635031%2Cn%3A3403933031%2Cp_89%3AAmazonBasics%2Cp_6%3AAT95IG9ONZD7S&dc&fst=as%3Aoff&pf_rd_i=6637738031&pf_rd_m=A1K21FY43GMZF8&pf_rd_p=3eb96131-76ce-41f7-9b72-4f5d8f986b21&pf_rd_r=M9KP9AE8GV7B4SS91K9M&pf_rd_s=merchandised-search-7&pf_rd_t=101&qid=1542006913&rnid=3403635031&suppress-ve=1&ref=s9_acss_bw_cg_abcatnav_9a1_w"
driver.get(website_URL)
myLinks = driver.find_elements_by_xpath("//*[@href]")
links = []
for link in myLinks:
    url = link.get_attribute("href")
    print(url)

As this code was printing all hrefs in the flipkart page, but I need only some required items of hrefs only.

  • 1
    Fix your question, format your code, dont shout, what error you get, if any. – Nikolay Barakov Jun 20 at 6:07
0

You can select WebElement's by using list comprehension with a filtering. For instance, if you want to select only links that have the Required text, you would:

links = driver.find_elements_by_xpath("//*[@href]")
required_links = [ link for link in links if "Required" in link.text]

The list comprehension says the following:

"Construct a new list made of each link in the links list if the text in this link has the Required sub-string"

If you have a different criteria, you just need to change the part after if.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.