Selenium 无法找到按钮的xpath
这是按钮的Selenium 无法找到按钮的xpath,selenium,xpath,selenium-webdriver,Selenium,Xpath,Selenium Webdriver,这是按钮的HTML: <button type="button" class="button" onclick="shippingMethod.save()"> <span> <span>Continue</span> </span> </button> 以及: 但是这两种方法都不起作用。尝试使用下面的任何一种XPath: 1-//按钮[@onclick='shippingMethod.save()'] 这
HTML
:
<button type="button" class="button" onclick="shippingMethod.save()">
<span>
<span>Continue</span>
</span>
</button>
以及:
但是这两种方法都不起作用。尝试使用下面的任何一种XPath: 1-
//按钮[@onclick='shippingMethod.save()']
这将定位具有
onclick
属性的按钮作为shippingMethod.save()
2-//按钮[规范化空格(.='Continue']
这将定位按钮,使innerHTML/文本完全等于
Continue
,并修剪空格(如果存在)
2-//按钮[@onclick='shippingMethod.save()'并规范化空格(.)='Continue']
这将定位具有
onclick
属性的按钮作为shippingMethod.save()
和与Continue
完全相同的innerHTML/text,并修剪空格(如果存在)。下面的一个定位器可以提供帮助
//按钮[text()='Continue']
-按子字符串查找
//按钮[@class='button'和@onclick='shippingMethod.save()]
-使用和
//按钮[@class='button'或@onclick='shippingMethod.save()]
-使用或
(//按钮[@type='button'])[1]
-使用索引,从1开始。尝试更改索引值
例如:(//按钮[@type='button'])[X]
这里X取索引值。您可以使用其中一个(最好是第二个,因为页面上可能有多个按钮与相同的条件相匹配): 或 此外,您还缺少一个标记名或*
By.xpath(".//[@id='shipping-method-buttons-container']/button")
将其更改为:
By.xpath("//*[@id='shipping-method-buttons-container']/button")
如果button是buttons容器元素的直接子元素,那么它应该可以工作。如果没有:
By.xpath("//*[@id='shipping-method-buttons-container']//button")
它们是从google chrome获取xpath的步骤 是否有任何异常?如果以下解决方案均不起作用,请共享周围的html代码以及错误(其他元素是否可用,请稍候,错误日志)。我认为xpath索引从1开始,而不是从0开始按钮的
text()
不会继续。。。它在SPAN
中。您的或
选项将与页面上的任何按钮匹配。
//span[.='Continue']/ancestor::button[@onclick='shippingMethod.save()']
By.xpath(".//[@id='shipping-method-buttons-container']/button")
By.xpath("//*[@id='shipping-method-buttons-container']/button")
By.xpath("//*[@id='shipping-method-buttons-container']//button")