Java Selenium Webdriver/Cucumber:基于隐藏元素执行操作

Java Selenium Webdriver/Cucumber:基于隐藏元素执行操作,java,selenium,Java,Selenium,我有一个带有以下html的按钮: 链接谷歌 取消谷歌链接以下是使用个人浏览器选择的更新版本: @When("^I check if the user has a \"([^\"]*)\" linked account$") public void linkChecker(String socialLoginXpathNotLinked) throws Throwable { driverWait = new WebDriverWait(driver, 20); driverWa

我有一个带有以下html的按钮:

链接谷歌

取消谷歌链接
以下是使用个人浏览器选择的更新版本:

@When("^I check if the user has a \"([^\"]*)\" linked account$")
public void linkChecker(String socialLoginXpathNotLinked) throws Throwable {
    driverWait = new WebDriverWait(driver, 20);
    driverWait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(socialLoginXpathNotLinked)));
    driverElement = driver.findElement(By.xpath(socialLoginXpathNotLinked));
    if(!driverElement.isDisplayed())
    {       
        isLinked = true;
    }   
}
@Then("^I unlink user if \"([^\"]*)\" linked$")
public void unlinkUser(String socialLoginXpathLinked) throws Throwable {
    if(isLinked)
    {           
       driver.findElement(By.xpath(socialLoginXpathLinked)).click();
       isLinked = false;
    }
}
我称之为浏览器初始化,这是JeffC建议的:

@Given("^I use \"([^\"]*)\" browser$")
public void browserInitialization(String browser) throws Throwable {
if (browser.equals("Chrome"))
{
    System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
    driver = new ChromeDriver();
}
else if(browser.equals("Firefox"))
{
    System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
    driver = new FirefoxDriver();
}
else if(browser.equals("Edge"))
{
    System.setProperty("webdriver.edge.driver", "C:\\Program Files (x86)\\Edge Driver\\MicrosoftWebDriver.exe");
    driver = new EdgeDriver();
}
else if(browser.equals("IE11"))
{
    System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer32.exe");
    driver = new InternetExplorerDriver();
}
}
在cucumber方面,我使用了一个场景大纲,我只是使用一个数据表进行浏览器抓取:

|Browser|
|Chrome|
|FireFox|
|Edge|
|IE11|

你好到目前为止你试过什么?我可能会先使用一个选择器来获取处于任一状态的元素,然后为if调用该元素上的
getText()
statement@mrfreester更新了答案,我要的不是文本,而是当前未显示的元素。哎呀,您真的不想为每个浏览器携带不同的
驱动程序
变量。创建驱动程序实例时,将该变量声明为
WebDriver
。然后,无论您驾驶的浏览器是什么,都可以使用它。这也将消除对每个驱动程序实例的不同等待的需要。我还建议您不要将
String
s作为定位器传递,而是通过
s传递
。例如,
public void unlinkUser(通过sociallinkator)
然后当您单击它时,
driver.findElement(sociallinkator)。click()
@JeffC不太清楚您的意思。我只使用了两个变量,并在Before注释中在测试执行时创建实例:Before public void websiteInitialization(){System.setProperty(“webdriver.chrome.driver”,“C:\\Program Files(x86)\\Google\\chrome\\Application\\chromedriver.exe”);chromedriver=new chromedriver();System.setProperty(“webdriver.ie.driver”,“C:\\ProgramFiles(x86)\\Internet Explorer\\IEDriverServer32.exe”);ieDriver=new InternetExplorerDriver();}标记格式似乎对我不起作用。我还使用了一个带有定位器的场景大纲数据表,因此如果它们发生更改,我只在数据表中更改一次,而不更改步骤定义。此外,我全局声明为步骤定义WebDriver ieDriver=null的启动;