【问题标题】:Getting href value of a tag of selenium web element获取硒网络元素标签的href值
【发布时间】:2015-09-03 07:11:10
【问题描述】:

我想获取标签链接的 url。我已将元素的类附加到在 python 中键入 selenium.webdriver.remote.webelement.WebElement:

elem = driver.find_elements_by_class_name("_5cq3")

html 是:

<div class="_5cq3" data-ft="{&quot;tn&quot;:&quot;E&quot;}">
    <a class="_4-eo" href="/9gag/photos/a.109041001839.105995.21785951839/10153954245456840/?type=1" rel="theater" ajaxify="/9gag/photos/a.109041001839.105995.21785951839/10153954245456840/?type=1&amp;src=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fhphotos-xfp1%2Ft31.0-8%2F11894571_10153954245456840_9038620401603938613_o.jpg&amp;smallsrc=https%3A%2F%2Fscontent.xx.fbcdn.net%2Fhphotos-prn2%2Fv%2Ft1.0-9%2F11903991_10153954245456840_9038620401603938613_n.jpg%3Foh%3D0c837ce6b0498cd833f83cfbaeb577e7%26oe%3D567D8819&amp;size=651%2C1000&amp;fbid=10153954245456840&amp;player_origin=profile" style="width:256px;">
        <div class="uiScaledImageContainer _4-ep" style="width:256px;height:394px;" id="u_jsonp_2_r">
            <img class="scaledImageFitWidth img" src="https://fbcdn-photos-h-a.akamaihd.net/hphotos-ak-prn2/v/t1.0-0/s526x395/11903991_10153954245456840_9038620401603938613_n.jpg?oh=15f59e964665efe28943d12bd00cefd9&amp;oe=5667BDBA&amp;__gda__=1448928574_a7c6da855842af4c152c2fdf8096e1ef" alt="9GAG's photo." width="256" height="395">
        </div>
    </a>
</div>

我希望 a 标签的 href 值落在 _5cq3 类中。

【问题讨论】:

    标签: python selenium xpath selenium-webdriver


    【解决方案1】:

    为什么不直接做呢?

    url = driver.find_element_by_class_name("_4-eo").get_attribute("href")
    

    如果你首先需要 div 元素,你可以这样做:

    divElement = driver.find_elements_by_class_name("_5cq3")
    url = divElement.find_element_by_class_name("_4-eo").get_attribute("href")
    

    或通过 xpath 的另一种方式(假设您的 5cq3 元素中只有一个链接元素:

    url = driver.find_element_by_xpath("//div[@class='_5cq3']/a").get_attribute("href")
    

    【讨论】:

    • 是否可以通过类 '_5c3' 访问它,因为有许多 '_5c3' 类,但它们都没有在 '_4-eo' 类中的链接。换句话说,'_4-eo' 只是完整 html 文档中的一个随机名称,但 '_5c3' 不是。我想要写在“_5c3”类中的链接
    • 更新了我的答案 ;-) 已经认为你会这样说,最好总是在你的问题中提供这种信息,这是必不可少的
    • 谢谢!请记住,下次我的问题会更具描述性:-)
    • 你的最后一行不应该是:url = driver.find_element_by_xpath("//div[@class='_5cq3']/a").get_attribute("href")
    【解决方案2】:

    你可以使用 xpath 来做同样的事情

    如果你想取“a”标签的href,根据你的HTML代码第二行然后使用

    url = driver.find_element_by_xpath("//div[@class='_5cq3']/a[@class='_4-eo']").get_attribute("href")
    

    如果你想取“img”标签的href,根据你的HTML代码第4行然后使用

    url = driver.find_element_by_xpath("//div[@class='_5cq3']/a/div/img[@class='scaledImageFitWidth img']").get_attribute("href")
    

    【讨论】:

      【解决方案3】:

      用途:

      1) xpath 首先指定href 的路径。

      x = '//a[@class="_4-eo"]'
      k = driver.find_elements_by_xpath(x).get_attribute("href")
      for url in k:
        print url
      

      2) 使用@drkthng 的解决方案(最简单的)。

      3)你可以使用:

      parentElement = driver.find_elements_by_class("_4-eo")
      elementList = parentElement.find_elements_by_tag_name("href")
      

      你可以在 Selenium 中使用任何你想要的东西。还有 2-3 种方法可以找到相同的。

      对于图片src,请在下方使用xpath

      img_path = '//div[@class="uiScaledImageContainer _4-ep"]//img[@src]'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-30
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 2019-07-28
        • 1970-01-01
        • 1970-01-01
        • 2022-12-09
        相关资源
        最近更新 更多