Selenium webdriver 我想在selenium中找到具有定位器内容的定位器类型
我想在selenium中找到具有定位器内容的定位器类型。下面是我编写的通过传递定位器内容来获取定位器类型的函数 当我执行Selenium webdriver 我想在selenium中找到具有定位器内容的定位器类型,selenium-webdriver,Selenium Webdriver,我想在selenium中找到具有定位器内容的定位器类型。下面是我编写的通过传递定位器内容来获取定位器类型的函数 当我执行Verify.java时,它将转到Function.java并从那里转到Element.java找到元素定位器类型并返回到函数,在函数中我将执行必要的操作,如sendkeys或click 在Verify.java中,我给出了文本框和按钮的Xpath。我的目的是去检查我传递的定位器内容是否属于哪个定位器 它通过检查第一个if本身来停止,并且不去catch块,也不移动到else i
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;
}
}
当未找到元素时,它不会循环,因为第一个(以下)条件会生成异常,并且代码会因您未处理它而中断
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 );
}
希望这对您有用。它不会循环,因为第一个(如下)条件在未找到元素时生成异常,并且代码因您未处理而中断
if (driver.findElement(By.id(LocatorContent)).isDisplayed())
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;
}
}
您应该通过检查字符串(定位器)来创建函数,如下所示:
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 );
}
希望这对您有用。请查找下面的更新代码,希望这能起作用
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;
}
}
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;
}
请找到下面更新的代码,希望这将工作
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;
}
嗨,Sadik,我不会给出“id=””或“css=”““。我将只传递代码中提到的值。然后我可以使用这个代码。在您的代码中,它会增加执行时间,因为它是按顺序查找元素的,假设您正在传递linkText,那么它将检查前面的条件,这将需要一些时间。因此,最糟糕的方法是在字符串(locator)中提到一些标志,然后在标识locator类型后应用find元素函数如果您使用了相同的代码,那么您应该在try catch blog中添加条件代码,如:try{if(driver.findelelement(By.id(LocatorContent)).isDisplayed(){elemen=driver.findElement(By.id(LocatorContent));System.out.println(“元素定位器是id”);}}catch(异常e){System.out.println(“未找到元素,定位器id为:+LocatorContent”);}return elemen;使用您提供的上述代码可以很好地工作,但它仅适用于id。如何将其用于xpath、css、linktext等各种定位器。在尝试中。您能告诉我上述代码是否适用于css、name、xpath和id,为linktext etcHi Sadik添加更多条件,我不会给出“id=””或“css=”““。我将只传递代码中提到的值。然后我可以使用此代码。在您的代码中,它会增加执行时间,因为它是按顺序查找元素的,假设如果您传递的是linkText,那么它将检查之前的条件,这将需要一些时间。因此,goof方法是在字符串(定位器)中提到一些标志在标识定位器类型后,应用查找元素函数如果您使用了相同的代码,那么应该在try catch blog中添加条件代码,如:try{if(driver.findElement(By.id(LocatorContent)).isDisplayed(){elemen=driver.findElement(By.id(LocatorContent));System.out.println(“元素定位器是id”);}}}catch(异常e){System.out.println(“未找到具有定位器id的元素:”+LocatorContent);}return elemen;它可以很好地处理您提供的上述代码,但它仅用于id。如何将它用于各种定位器,如xpath、css、linktext..在尝试中。您能告诉我上述代码是否适用于css、name、xpath和id,为linktext等添加更多条件吗