Java @FindBy找不到元素,但findElement可以在salesforce中找到它
我正在使用BDD Maven框架,使用Selenium Webdriver测试Salesforce应用程序 此外,在几页工作正常后,我面临以下问题: 我的PageFactory(PagePackage)正在使用注释@FindBy,但是Selenium找不到要交互的WebElement,但是我创建了另一个项目来测试driver.findElement(By.xpath…)是否有效,Selenium可以轻松找到WebElement 使用@FindBy,我收到错误:Java @FindBy找不到元素,但findElement可以在salesforce中找到它,java,maven,selenium-webdriver,Java,Maven,Selenium Webdriver,我正在使用BDD Maven框架,使用Selenium Webdriver测试Salesforce应用程序 此外,在几页工作正常后,我面临以下问题: 我的PageFactory(PagePackage)正在使用注释@FindBy,但是Selenium找不到要交互的WebElement,但是我创建了另一个项目来测试driver.findElement(By.xpath…)是否有效,Selenium可以轻松找到WebElement 使用@FindBy,我收到错误: [31morg.openqa.se
[31morg.openqa.selenium.ElementNotInteractableException: element not interactable
我还有另外一个使用@FindBy的页面没有问题,使用相对或绝对Xpath,效果很好,但特别是在这个页面中,它不起作用。
正如您在下面看到的,我尝试使用javascript和默认方法单击
页面类:
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import Comgas.PoCProspect.util.driverContext;
public class EtapaC_DebitoPage {
public EtapaC_DebitoPage() {
PageFactory.initElements(driverContext.getDriver(), this);
}
@FindBy(how = How.XPATH, using = "//input[@type='checkbox'][@data-interactive-lib-uid='3']")
protected WebElement checkBox;
import org.openqa.selenium.By;
import aaa.PoCProspect.page.EtapaC_DebitoPage;
import aaa.PoCProspect.util.driverContext;
public class EtapaC_DebitoAction extends EtapaC_DebitoPage {
protected void clickEditAutorizaDebtAutom() {
//driverContext.getDriver().findElement(By.xpath("chkAutorizaDebtAutom")).click();
chkAutorizaDebtAutom.click();
}
}
import org.openqa.selenium.JavascriptExecutor;
import aaa.PoCProspect.action.EtapaC_DebitoAction;
import aaa.PoCProspect.stepDef.util.util;
import aaa.PoCProspect.util.driverContext;
import io.cucumber.java.en.And;
public class EtapaC_DebitoStepDef extends EtapaC_DebitoAction{
@And ("^Click on the checkbox$")
public void AutorizaDebAutomatico() throws InterruptedException {
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor)driverContext.getDriver();
js.executeScript("arguments[0].click();", chkAutorizaDebtAutom); //This is a try to click with Java
Thread.sleep(1000);
clickEditAutorizaDebtAutom(); // this is a default click action from framework
}
}
package Selenium.UnitTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class App {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options); //input objeto options na instanciação do driver
String baseUrl = "https://salesforcerandomapp.com";
// String tagName = "";
// String divName = "";
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.xpath("//div[text()='Editar']")).click();
}
}
动作类:
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import Comgas.PoCProspect.util.driverContext;
public class EtapaC_DebitoPage {
public EtapaC_DebitoPage() {
PageFactory.initElements(driverContext.getDriver(), this);
}
@FindBy(how = How.XPATH, using = "//input[@type='checkbox'][@data-interactive-lib-uid='3']")
protected WebElement checkBox;
import org.openqa.selenium.By;
import aaa.PoCProspect.page.EtapaC_DebitoPage;
import aaa.PoCProspect.util.driverContext;
public class EtapaC_DebitoAction extends EtapaC_DebitoPage {
protected void clickEditAutorizaDebtAutom() {
//driverContext.getDriver().findElement(By.xpath("chkAutorizaDebtAutom")).click();
chkAutorizaDebtAutom.click();
}
}
import org.openqa.selenium.JavascriptExecutor;
import aaa.PoCProspect.action.EtapaC_DebitoAction;
import aaa.PoCProspect.stepDef.util.util;
import aaa.PoCProspect.util.driverContext;
import io.cucumber.java.en.And;
public class EtapaC_DebitoStepDef extends EtapaC_DebitoAction{
@And ("^Click on the checkbox$")
public void AutorizaDebAutomatico() throws InterruptedException {
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor)driverContext.getDriver();
js.executeScript("arguments[0].click();", chkAutorizaDebtAutom); //This is a try to click with Java
Thread.sleep(1000);
clickEditAutorizaDebtAutom(); // this is a default click action from framework
}
}
package Selenium.UnitTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class App {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options); //input objeto options na instanciação do driver
String baseUrl = "https://salesforcerandomapp.com";
// String tagName = "";
// String divName = "";
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.xpath("//div[text()='Editar']")).click();
}
}
步骤定义类:
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import Comgas.PoCProspect.util.driverContext;
public class EtapaC_DebitoPage {
public EtapaC_DebitoPage() {
PageFactory.initElements(driverContext.getDriver(), this);
}
@FindBy(how = How.XPATH, using = "//input[@type='checkbox'][@data-interactive-lib-uid='3']")
protected WebElement checkBox;
import org.openqa.selenium.By;
import aaa.PoCProspect.page.EtapaC_DebitoPage;
import aaa.PoCProspect.util.driverContext;
public class EtapaC_DebitoAction extends EtapaC_DebitoPage {
protected void clickEditAutorizaDebtAutom() {
//driverContext.getDriver().findElement(By.xpath("chkAutorizaDebtAutom")).click();
chkAutorizaDebtAutom.click();
}
}
import org.openqa.selenium.JavascriptExecutor;
import aaa.PoCProspect.action.EtapaC_DebitoAction;
import aaa.PoCProspect.stepDef.util.util;
import aaa.PoCProspect.util.driverContext;
import io.cucumber.java.en.And;
public class EtapaC_DebitoStepDef extends EtapaC_DebitoAction{
@And ("^Click on the checkbox$")
public void AutorizaDebAutomatico() throws InterruptedException {
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor)driverContext.getDriver();
js.executeScript("arguments[0].click();", chkAutorizaDebtAutom); //This is a try to click with Java
Thread.sleep(1000);
clickEditAutorizaDebtAutom(); // this is a default click action from framework
}
}
package Selenium.UnitTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class App {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options); //input objeto options na instanciação do driver
String baseUrl = "https://salesforcerandomapp.com";
// String tagName = "";
// String divName = "";
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.xpath("//div[text()='Editar']")).click();
}
}
这是有效的代码:
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import Comgas.PoCProspect.util.driverContext;
public class EtapaC_DebitoPage {
public EtapaC_DebitoPage() {
PageFactory.initElements(driverContext.getDriver(), this);
}
@FindBy(how = How.XPATH, using = "//input[@type='checkbox'][@data-interactive-lib-uid='3']")
protected WebElement checkBox;
import org.openqa.selenium.By;
import aaa.PoCProspect.page.EtapaC_DebitoPage;
import aaa.PoCProspect.util.driverContext;
public class EtapaC_DebitoAction extends EtapaC_DebitoPage {
protected void clickEditAutorizaDebtAutom() {
//driverContext.getDriver().findElement(By.xpath("chkAutorizaDebtAutom")).click();
chkAutorizaDebtAutom.click();
}
}
import org.openqa.selenium.JavascriptExecutor;
import aaa.PoCProspect.action.EtapaC_DebitoAction;
import aaa.PoCProspect.stepDef.util.util;
import aaa.PoCProspect.util.driverContext;
import io.cucumber.java.en.And;
public class EtapaC_DebitoStepDef extends EtapaC_DebitoAction{
@And ("^Click on the checkbox$")
public void AutorizaDebAutomatico() throws InterruptedException {
Thread.sleep(5000);
JavascriptExecutor js = (JavascriptExecutor)driverContext.getDriver();
js.executeScript("arguments[0].click();", chkAutorizaDebtAutom); //This is a try to click with Java
Thread.sleep(1000);
clickEditAutorizaDebtAutom(); // this is a default click action from framework
}
}
package Selenium.UnitTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class App {
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
// Optional, if not specified, WebDriver will search your path for chromedriver.
System.setProperty("webdriver.chrome.driver", "drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options); //input objeto options na instanciação do driver
String baseUrl = "https://salesforcerandomapp.com";
// String tagName = "";
// String divName = "";
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.xpath("//div[text()='Editar']")).click();
}
}
我希望@FindBy可以找到我的WebElement,框架会点击复选框。这只适用于最后显示的使用findElement的selenium代码