【问题标题】:Python Selenium - Find Input Field after LabelPython Selenium - 在标签后查找输入字段
【发布时间】:2021-06-16 17:57:27
【问题描述】:

我需要在标签之后找到一个输入字段。 Id 和 Name 是动态的,每次登录都会改变。

HTML:

<label for="EveryTimeDifferent">LabelText</label>
<div>
    <div>
        <input name="EveryTimeDifferent" id="EveryTimeDifferent">
    </div>
</div>

Python:

driver.find_element_by_xpath("//label[text()='LabelText']//following::input[1]")

【问题讨论】:

  • 您的 XPath(或简化版 //label[text()='LabelText']/following::input)应该可以工作。也分享异常日志
  • 嗨 JaSON,谢谢你的权利,现在我看到元素已找到,但 Send_Keys 不起作用。我的尝试: test = driver.find_element_by_xpath("//label[text()='LabelText']/following::input") print(test.tag_name) test.send_keys("Test")
  • Python 打印出标签名称输入,只要正确,但 send_keys 被跳过。
  • 可能有多个输入字段。检查print(len(driver.find_elements_by_xpath("//label[text()='LabelText']/following::input")))。是打印1吗?你需要选择一个当前可见的
  • JaSON 非常感谢你,现在可以使用了 :)

标签: python selenium xpath dynamic


【解决方案1】:

首先,您需要在给定网站上找到每次会话都保持不变的内容。我假设给定示例中的所述标签中的某些内容保持不变。您可以使用 BeautifulSoup 并使用 bs4 的 HTML 解析函数找到标签(您需要从网站创建 HTML 代码的本地字符串表示,以便创建 BeautifulSoup() 实例)。 应该是这样的:

from bs4 import BeautifulSoup

soup = BeautifulSoup(HTML_SRC_STR, 'html.parser')
soup.find('<your_label_tag'>' ...)

bs4 的文档会更好地解释它:https://www.crummy.com/software/BeautifulSoup/bs4/doc/

【讨论】:

    猜你喜欢
    • 2016-04-24
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 2019-12-01
    • 2011-10-14
    • 1970-01-01
    相关资源
    最近更新 更多