【问题标题】:Get a link text and element text based on a condition根据条件获取链接文本和元素文本
【发布时间】:2019-02-02 13:26:57
【问题描述】:

如果有'theme-cell-card Ace',我必须获取元素的文本和链接,否则没有。以下是示例 html 代码:

<div class="theme-grid-cell-frame">
    <a href="/t/490">
        <div class="theme-cell">
            <div class="image"></div>
            <div class="theme-cell-overlay deep"></div>
            <h1 class="theme-cell-name"> textqwqw</h1>
            <div class="theme-cell-card Ace"></div>
        </div>
    </a>
</div>
<div class="theme-grid-cell-frame">
    <a href="/o/434">
        <div class="theme-cell">
            <div class="image"></div>
            <div class="theme-cell-overlay deep"></div>
            <h1 class="theme-cell-name"> textegg</h1>
            <div class="theme-cell-card Jack"></div>
        </div>
    </a>
</div>    
<div class="theme-grid-cell-frame">
    <a href="/t/4665">
        <div class="theme-cell">
            <div class="image"></div>
            <div class="theme-cell-overlay deep"></div>
            <h1 class="theme-cell-name"> textdgfh</h1>
            <div class="theme-cell-card Ace"></div>
        </div>
    </a>
</div>
<div class="theme-grid-cell-frame">
    <a href="/o/764">
        <div class="theme-cell">
            <div class="image"></div>
            <div class="theme-cell-overlay deep"></div>
            <h1 class="theme-cell-name"> textgrth</h1>
        </div>
    </a>
</div>

我能够获取元素的文本,但我想通过条件 class="theme-cell-card Ace" 为真。

${grid}     Set Variable    //div[@class='theme-cell']
@{elements}    Get Webelements    ${grid}
:FOR   ${element}    IN    @{elements}
\    ${text}    Get Text    ${element}

我是新手,如果您需要更多信息,请告诉我。谢谢

【问题讨论】:

    标签: python selenium-webdriver robotframework


    【解决方案1】:
            @{elements}    Get Webelements    //a[.//div[@class='theme-cell-card Ace']]
            :FOR   ${element}    IN    @{elements}
            \        ${text}    Get Text    ${element}
            \        ${link}    SeleniumLibrary.Get Element Attribute    ${element}    attribute=href
            \        Log to console     ${text}
            \        Log to console     ${link}
    

    【讨论】:

      【解决方案2】:

      我不知道机器人框架,但这是你想要的 XPath 定位器

      //a[.//div[@class='theme-cell-card Ace']]
      

      这将为您提供包含DIVA 标记,该标记具有所需的类。您可以从该元素中获取 href 以及包含的文本。

      由于你的问题被标记为 python,你可以使用一些简单的东西,比如

      aces = driver.find_elements_by_xpath("//a[.//div[@class='theme-cell-card Ace']]")
      for ace in aces
          print(ace.get_attribute("href"))
          print(ace.text)
      

      【讨论】:

      • stackoverflow.com/users/2386774/jeffc 嗯,感谢您的调查。我知道 'theme-cell-card Ace' 类的 xpath/css。但是如何根据这个条件获取链接和文本呢?
      • 您的 XPath 定位器与我的不同。你会找到一个DIV 我的找到你想要的A 标签。从那里你可以得到A标签的href来获取链接,或者你可以得到包含的文本......这都是你想要的。我不确定问题是什么。
      • 我添加了一个简单的 python 示例来展示如何在循环中从A 标记中获取href 和文本。这就是你要找的吗?
      • JeffC,漂亮谢谢。非常感谢 xpath 和示例代码。他们帮助了我。主要问题是xpath。我将在 Robotframework 中添加解决方案
      • JeffC,我对您的回答投了赞成票,但由于我的声誉得分低于 15,因此它没有出现。谢谢你。
      猜你喜欢
      • 1970-01-01
      • 2012-09-04
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2011-03-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多