【问题标题】:python selenium search for exact text in web tablepython selenium 搜索 web 表中的确切文本
【发布时间】:2021-04-08 20:01:17
【问题描述】:

在尝试使用 Selenium 和 Python 在 web 表中定位确切文本时,我正在努力寻找语法。

表 xpath 是:

driver.find_element_by_xpath('//*[@id="someId"]/table[2]')

有 4 列和未知的行数。我只对 First 列感兴趣。 3 行的示例 xpath:

//*[@id="sameValue"]/table[2]/tbody/tr[3]/td[1]/input
//*[@id="sameValue"]/table[2]/tbody/tr[4]/td[1]/input
//*[@id="sameValue"]/table[2]/tbody/tr[5]/td[1]/input

基本上,我在第一列中有单选按钮,我想点击第一列中具有 MY VALUE 的行。不知道是第4排还是第26排。我知道这是第一列。一些额外的细节:

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="some Value">
</td>

<td class="class value" width="70">
<input type="radio" name="FileName" value="MY VALUE">
</td>

还有一件事我正在寻找的名字是可变的。我猜语法会是一些 F 字符串,所以如果可以的话,请给我一些提示。

感谢您的时间、知识和努力。非常感谢他们

【问题讨论】:

    标签: python selenium xpath webdriver


    【解决方案1】:

    要单击 value 属性为 value="MY VALUE" 的输入元素,请使用以下 xpath 之一。

    //*[@id='sameValue']/table[2]/tbody//tr//td[1]/input[@value='MY VALUE']
    

    或者

    //input[@value='MY VALUE']
    

    在 python 中,您可以使用 python format() 函数。

    strvar="MY VALUE"
    driver.find_element_by_xpath("//*[@id='sameValue']/table[2]/tbody//tr//td[1]/input[@value='{}']".format(strvar)).click()
    

    【讨论】:

    • 谢谢昆都克。非常感谢您的帮助。这正是我正在努力解决的问题。
    【解决方案2】:

    尝试通过值文本定位 xpath:

    driver.find_element_by_xpath("//input[@value='MY VALUE']")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-01-23
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2017-01-28
      • 2016-01-26
      • 1970-01-01
      相关资源
      最近更新 更多