简体   繁体   English

如何通过类名识别元素并使用Selenium和Java单击它?

[英]How to identify an element through classname and click on it using Selenium and Java?

在此处输入图片说明

Click button via class because it has no ID . 通过类单击按钮,因为它没有ID。 Or via value? 还是通过价值?

tried className , cssSelector , partialLinkText and LinkText but sadly did not work clicking the save button 尝试了className,cssSelector,partialLinkText和LinkText,但遗憾的是,单击“保存”按钮后无法正常工作

System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.className("bttn-positive save-button"));
save.click();

Should be able to click save button 应该可以点击保存按钮

we can not use the multiple class name in the className locator. 我们不能在className定位器中使用多个类名。 So, you can use the XPath locator with the multiple class name as below ( //input[@class='bttn-positive save-button'] ) 因此,您可以将XPath定位器与以下多个类名一起使用( //input[@class='bttn-positive save-button']

Code: 码:

System.out.println("Succesful in Saving Product ");
WebElement save = driver.findElement(By.xpath("//input[@class='bttn-positive save-button']"));
save.click();

You can't pass multiple classnames while using driver.findElement(By.className("bttn-positive save-button")) and doing so you will face Invalid selector: Compound class names not permitted error. 使用driver.findElement(By.className("bttn-positive save-button")) ,您不能传递多个类名 ,这样做将面临无效的选择器:复合类名不允许错误。

To click() on the green button with text as Save you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies : 要在带有文本的另存为的绿色按钮上click() ,必须为elementToBeClickable()引入WebDriverWait ,并且可以使用以下定位策略之一

  • cssSelector : cssSelector

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.bttn-positive[value^='Save'][type='button']"))).click(); 
  • xpath : xpath

     new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[contains(@class, 'bttn-positive') and starts-with(@value, 'Save')][@type='button']"))).click(); 

Try save.submit(); 尝试save.submit();

Submit buttons are used to submit the entire form to the server. 提交按钮用于将整个表单提交到服务器。 We can either use the click () method on the web element like a normal button as we have done above or use the submit () method on any web element in the form or on the submit button itself. 我们可以像上面常规按钮一样在Web元素上使用click()方法,或者在表单中的任何Web元素上或在Submit按钮本身上使用Submit()方法。

In this case "save.click()" will be work, but some time to save any product on any app like eCommerce or Banking domain it will not working properly & one more important think click () cause a new page to load, this method will attempt to load the page . 在这种情况下,“ save.click()”是可行的,但是需要一段时间将任何产品保存在任何应用程序(例如电子商务或银行领域)上,否则它将无法正常工作,更重要的是,认为click()会导致加载新页面方法将尝试加载页面。 So better to use "save.submit()" if the current elements is form Or with in the form the this will be submitted. 因此,如果当前元素为form或将以this的形式提交,则最好使用“ save.submit()”。 As up ur requirements submit () one is the better option. 由于您的要求Submit()是更好的选择。

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

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