我想在selenium中找到具有定位器内容的Locator类型。以下是我通过传递定位器内容来获取定位器类型的函数。
当我执行Verify.java
时,它将转到Function.java
,然后从Element.java
找到元素定位器类型并返回到函数,并且在函数中我将执行必要的操作像sendkeys或click一样的操作。
在Verify.java
我已经给出了文本框的Xpath和按钮。我的目的是去检查我传递的定位器内容是否属于哪个定位器。
它通过检查第一个if
本身停止而不是catch
阻止而不是移动到else if
来验证其他定位器类型。如果我从第一个if
注释到Xpath,如果它正常工作。它不是循环和检查。
有人能建议我解决方案吗?
(Testcase) Verify.Java
----------------------
package cm;
import org.testng.annotations.Test;
public class Verify extends Function{
@Test
public void Check(){
Browser("Chrome", "https://www.google.co.in");
Enter("//*[@id='lst-ib']", "Karthick");
Click(".//*[@id='sblsbb']");
}
}
Function.Java
-------------
package cm;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Function extends Element{
public void Enter(String LocatorContent, String Value) {
FindElement(LocatorContent).sendKeys(Value);
}
public void Click(String LocatorContent) {
FindElement(LocatorContent).click();
}
}
Element.Java
------------
package cm;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Element extends Browser {
public Element()
{
this.driver = driver;
}
public WebElement FindElement(String LocatorContent){
//this.driver = driver;
WebElement elemen = null;
if (driver.findElement(By.id(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.id(LocatorContent));
System.out.println("element locator is id");
} catch (Exception e) {
System.out.println("Element Not Found given with Locator Id : "+LocatorContent);
}
return elemen;
}
else if (driver.findElement(By.name(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.name(LocatorContent));
} catch (Exception e) {
System.out.println("Element Not Found given with Locator Name : "+LocatorContent);
}
return elemen;
}
else if (driver.findElement(By.className(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.className(LocatorContent));
} catch (Exception e) {
System.out.println("Element Not Found given with Locator ClassName : "+LocatorContent);
}
return elemen;
}
else if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.cssSelector(LocatorContent));
} catch (Exception e) {
System.out.println("Element Not Found given with Locator Css : "+LocatorContent);
}
return elemen;
}
else if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.xpath(LocatorContent));
System.out.println("It Found the element "+LocatorContent);
} catch (Exception e) {
System.out.println("Element Not Found given with Locator Xpath : "+LocatorContent);
}
return elemen;
}
else if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) {
try {
elemen = driver.findElement(By.linkText(LocatorContent));
} catch (Exception e) {
System.out.println("Element Not Found given with Locator LinkText : "+LocatorContent);
}
return elemen;
}
else if(driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) {
{
try {
elemen = driver.findElement(By.partialLinkText(LocatorContent));
} catch (Exception e) {
System.out.println("Element Not Found given with Locator PartialLinkText : "+LocatorContent);
}
return elemen;
}
}
return elemen;
}
}
答案 0 :(得分:0)
它不会循环为第一个(下面)条件在未找到元素时生成异常并且代码中断,因为您还没有处理它。
if (driver.findElement(By.id(LocatorContent)).isDisplayed())
您应该通过检查字符串(定位符)来创建函数,如下所示:
public WebElement ByLocator(String locator) {
By result = null;
if (locator.startsWith("//")) {
result = By.xpath(locator);
} else if (locator.startsWith("css=")) {
result = By.cssSelector(locator.replace("css=", ""));
} else if (locator.startsWith("id=")) {
result = By.id(locator.replace("id=", ""));
} else{
result = By.name(locator);
}
return driver.findElement(result );
}
希望这对你有用。
答案 1 :(得分:0)
Latest edited
-------------
package cm;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class Ele extends Browser {
public WebElement FindElement(String LocatorContent) throws Exception{
this.driver = driver;
WebElement elemen = null;
try {
if (driver.findElement(By.id(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.id(LocatorContent));
System.out.println("element locator is id");
}
else if (driver.findElement(By.name(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.name(LocatorContent));
System.out.println("element locator is name");
}
else if (driver.findElement(By.className(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.className(LocatorContent));
System.out.println("element locator is className");
}
else if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.cssSelector(LocatorContent));
System.out.println("element locator is cssSelector");
}
else if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.linkText(LocatorContent));
System.out.println("element locator is linkText");
}
else if (driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.partialLinkText(LocatorContent));
System.out.println("element locator is partialLinkText");
}
else if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.xpath(LocatorContent));
System.out.println("element locator is xpath");
}
}
catch (Exception e) {
if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.xpath(LocatorContent));
System.out.println("Element locator is xpath");
}
//System.out.println("Element Not Found given with Locator : "+LocatorContent);
}
return elemen;
}
}
答案 2 :(得分:0)
请在下面找到更新的代码,希望这会有效。
public WebElement FindElement(String LocatorContent) throws Exception{
this.driver = driver;
WebElement elemen = null;
try {
if (driver.findElement(By.id(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.id(LocatorContent));
System.out.println("element locator is id");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.name(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.name(LocatorContent));
System.out.println("element locator is name");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.className(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.className(LocatorContent));
System.out.println("element locator is className");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.cssSelector(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.cssSelector(LocatorContent));
System.out.println("element locator is cssSelector");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.linkText(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.linkText(LocatorContent));
System.out.println("element locator is linkText");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.partialLinkText(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.partialLinkText(LocatorContent));
System.out.println("element locator is partialLinkText");
return elemen;
}
}
catch(Exception ex){ }
try {
if (driver.findElement(By.xpath(LocatorContent)).isDisplayed()) {
elemen = driver.findElement(By.xpath(LocatorContent));
System.out.println("element locator is xpath");
return elemen;
}
}
catch (Exception e) { }
if (elemen.equals(null))
throw new Exception("Please not found");
return elemen;
}