Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium 无法找到按钮的xpath_Selenium_Xpath_Selenium Webdriver - Fatal编程技术网

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") 
  • 在网页上的元素上单击鼠标右键,然后单击“检查”
  • 应该突出显示元素的HTML
  • 右键单击突出显示的部分
  • 将鼠标悬停在“复制”上
  • 单击复制xpath

  • 它们是从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")