使用Selenium python查找span类

问题描述 投票:0回答:1

我正在使用selenium和python来学习自动化Web测试。

我想单击“继续”按钮,虽然其中只有跨度(我已经知道使用id代替跨度非常容易)但在这种情况下,我要单击范​​围。

我正在使用以下代码:

driver.find_element_by_xpath("//span[contains(@class,'mdBtn01Txt')][contains(text(),'Continue')]").click()

这里是元素:

<div class="mdLYR12Inner01">
<a class="MdBtn01 mdBtn01 ExDisabled FnSubmitBtn" style="display:none" href="#" onclick="charge(this); return false;">
<span class="mdBtn01Inner">
<span class="mdBtn01Txt">
Continue <<<(I want to click this button)
</span>
</span>
</a>    
</div>
</footer>

但是,我在下面收到此消息:

raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[contains(@class,'mdBtn01Txt')][contains(text(),'Continue')]"}
python python-3.x python-2.7 selenium
1个回答
3
投票

看起来不像有效的xpath,正确的应该是

//span[@class='mdBtn01Txt']

这里是创建xpath的规则XPath的语法:

XPath包含位于网页上的元素的路径。创建XPath的标准语法为:

Xpath=//tagname[@attribute='value']

  • 标记名:特定节点的标记名。
  • @:选择属性。
  • 属性:节点的属性名称。
  • 值:属性的值。
© www.soinside.com 2019 - 2024. All rights reserved.