简体   繁体   中英

Finding href link using python selenium xpath

So I want to get the href from < p > tag using xpath.

I want to use the text from < h1 > tag ('Cable Stripe Knit L/S Polo') and simultaneously text from < p > tag ('White') to find the href in the < p > tag

. .

Note: There are more colors of one item (more articles with different < p > tags but same < h1 > tag)!

Html Source

<article>
    <div class="inner-article">
       <a href="/shop/tops-sweaters/ix4leuczr/a1ykz7f2b" style="height:150px;">
       </a>

           <h1>
              <a href="/shop/tops-sweaters/ix4leuczr/a1ykz7f2b" class="name-link">Cable Stripe Knit L/S Polo
              </a>
           </h1>

                 <p>
                    <a href="/shop/tops-sweaters/ix4leuczr/a1ykz7f2b" class="name-link">White</a>
                 </p>
    </div>
</article>

I've tried this code but it doesn't work

specificProductColor = driver.find_element_by_xpath("//div[@class='inner-article' and contains(text(), 'White') and contains(text(), 'Cable')]/p")

driver.get(specificProductColor.get_attribute("href"))

Much thanks for your response!

As per the html source the xpath to get the href tags would be something like this:

specificProductColors = driver.find_elements_by_xpath("//div[@class='inner-article']//a[contains(text(), 'White') or contains(text(), 'Cable')]")

specificProductColors[0].get_attribute("href")

specificProductColors[1].get_attribute("href")

Since there are 2 hyperlink tags, you should be using find_elements_by_xpath which returns list of elements. In this case it would return 2 hyperlink tags, and you could get their href using get_attribute method.

I've got a working code. It's not the fastest one - this part takes ~550 ms but it works. If someone could simplify that, I'd be very thankful :)

.

It takes all products with specified keyword (Cable) from product page and all products with specified color (White) from product page aswell. It compares href links and matches wanted product with wanted color.

.

Also want to simplify the loop - stop both for loops if the links match

specificProduct = driver.find_elements_by_xpath("//div[@class='inner-article']//*[contains(text(), '"+ productKeyword[arrayCount] +"')]")
specificProductColor = driver.find_elements_by_xpath("//div[@class='inner-article']//*[contains(text(), '"+ desiredColor[arrayCount] +"')]")



for i in specificProductColor: 
    specProductColor = i.get_attribute("href")
    for i in specificProduct: 
        specProduct = i.get_attribute("href")
        if specProductColor == specProduct:
            print(specProduct) 
            wantedProduct = specProduct


driver.get(wantedProduct)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM