以下元素的XPath是什么?

2024-10-03 00:28:27 发布

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

我需要找到以下元素的XPath:

<a> href="/resident/register/">Register another device </a>

我以为解决办法是

$x("//*[contains(@href, 'resident/register')]")

但这并没有带来任何回报。有什么想法吗?你知道吗


Tags: register元素deviceanotherxpathhrefcontainsresident
2条回答

从@kjhughes开始,解决了HTML的一个问题,即HTML的格式不正确,因为您必须根据自己的理解提供正确的HTML。你知道吗

实际的HTML我想如下:

<a href="/resident/register/">Register another device </a>

因此,元素的有效XPath可以是以下任一项:

  • XPath:1

    "//a[contains(@href, '/resident/register') and contains(.,'Register another device')]"
    
  • XPath:2

    "//a[contains(@href, '/resident/register') and normalize-space()='Register another device']"
    

你的HTML格式不正确。你知道吗

改变

<a> href="/resident/register/">Register another device </a>

<a href="/resident/register/">Register another device </a>

然后XPath将按预期工作。你知道吗

如果HTML是固定的,则必须调整XPath以测试元素内容,而不是href属性内容:

//a[contains(.,'resident/register')]

但是,尽管这可以选择格式错误的a元素,但由于它缺少适当的href属性,因此无法单击。你知道吗

相关问题 更多 >