使用python selenium xpath查找href链接

2024-09-27 17:35:52 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我希望使用xpath从<;p>;获取href标记。在

我想使用文本来自<;h1>;标签('Cable Stripe Knit L/S Polo') 同时<;p>;标记('White')中查找<;p>;标记中的href

一。 . 在

注意:一个项目有更多的颜色(更多的文章具有不同的<;p>;标签,但<;h1>;标签相同)!在

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

^{pr2}$

Much thanks for your response!


Tags: 标记ltgtarticle标签shoph1class
2条回答

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)

根据html源代码,用于获取href标记的xpath如下所示:

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")

因为有2个超链接标记,所以应该使用find_elements_by_xpath,它返回元素列表。在本例中,它将返回2个超链接标记,您可以使用get_attribute方法获取它们的href。在

相关问题 更多 >

    热门问题