简体   繁体   English

如何在硒webdriver中找到xpath

[英]how to find xpath in selenium webdriver

在此处输入图片说明 Following is the HTML of the page, How do i get the Xpath or is there anyother way to automate using java?In fact, we shud click on this "continue" button. 以下是页面的HTML,如何获取Xpath或使用Java进行自动化的其他方法?实际上,我们单击了“继续”按钮。

 <regform-button>
<button ng-disabled="activityIndicator" ng-click="validate()" type="button">
<div template="api-loader" ng-http-loader="">
<div class="http-loader__wrapper ng-hide" ng-show="showLoader" ng-include="template">
<span class="api-loader"></span>
</div>
</div>
<ng-transclude>
</button>
</regform-button>

If you are using Google Chrome, right click on the button, and select Inspect in the popup. 如果您使用的是Google Chrome浏览器,请右键单击该按钮,然后在弹出窗口中选择“检查”。 The html will open in a developer tools frame. html将在开发人员工具框架中打开。 Right click on the element in the developer tools frame, hover over copy, select copy xpath. 右键单击开发人员工具框架中的元素,将鼠标悬停在副本上,然后选择副本xpath。

Here is the XPath to the form on the URL. 这是URL上表单的XPath。

//*[@id="main-frame"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button // * [@@ id =“ main-frame”] / div [1] / div [2] / div / div [1] / div [1] / form / regform-steps / ng-transclude / regform-step [ 1] / ng-form / ng-transclude / regform-b​​utton / button

    WebDriver driver = new ChromeDriver();
    driver.get("https://uk.match.com/unlogged/landing/2016/06/02/hpv-belowthefold-3steps-geo-psc-bowling?klid=6740");
    //fill in fields
    WebElement element = driver.findElement(By.xpath("//*[@id=\"main-frame\"]/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button"));
    element.click();
driver.findElement(By.xpath("//regform-button/button")).click();

Try opening the console for Google Chrome and typing the following: document.evaluate("//regform-button/button", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.click() 尝试打开Goog​​le Chrome浏览器的控制台并输入以下内容:document.evaluate(“ // regform-b​​utton / button”,document,null,XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.click()

It works...that's if you've specified the required fields. 它有效...就是您指定了必填字段。 Also be sure to wait for a while (2 secs to be sure) after specifying the required fields as there might be an additional load after setting the required fields. 在指定必填字段后,还请确保稍等片刻(确保2秒钟),因为设置必填字段后可能会产生额外的负担。

Edit: The code is javascript..I just took the xpath given by the previous answers and used that to locate the element through js. 编辑:代码是javascript ..我只是采用了先前答案给出的xpath,并使用它通过js来定位元素。 I'm saying that "//regform-button/button" should work.. 我是说“ // regform-b​​utton / button”应该起作用。

从“ 继续”按钮之前出现的下拉列表中选择所需的值后,可以使用以下语句单击“ 继续”按钮:

 driver.findElement(By.xpath("//span[text()='Continue']")).click();

The following xpath worked for me.But I dont know, why is it different for each different browser. 以下xpath为我工作。但是我不知道,为什么每个浏览器都不一样。

For Firefox : 对于Firefox:

`driver.findElement(By.xpath("//*[@id='main-frame']/div[1]/div[2]/div/div[1]/div[1]/form/regform-steps/ng-transclude/regform-step[1]/ng-form/ng-transclude/regform-button/button")).click()`;

For chrome: driver.findElement(By.xpath("//regform-button/button")).click(); 对于chrome: driver.findElement(By.xpath("//regform-button/button")).click();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM