2

这让我发疯,我尝试过使用By.Id or By.Xpath但没有任何效果,这是我Select Method试图从下拉列表中选择文本

//测试代码

 SelectMyText(By.XPath("//*[@id='ctl00_ContentPlaceHolder1_ddlCategory']"), "Employee");

//方法:

     public void SelectMyText(By locator, string valueToBeSelected)
    {
        var options = GetElementId(locator).FindElements(By.TagName("option"));
        foreach (var option in options)
        {
            if (valueToBeSelected == option.Text)
            {
                option.Click();
                return;
            }
        }
    }

    public IWebElement GetElementId(By locator)
    {
        return Driver.FindElement(locator);
    }

这是得到的错误:

threw exception: 
System.NotImplementedException: Element is no longer valid
    at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
    at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebDriver.InternalExecute(String driverCommandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
    at OpenQA.Selenium.Remote.RemoteWebElement.get_Text()

以下是此错误的屏幕截图:

在此处输入图像描述 在此处输入图像描述

4

2 回答 2

2

这个想法很简单。当您怀疑元素不可访问、不可见等时,您可以使用 js 来欺骗它)所以下面我们有一些方法: -getText -clickontheelement -getelementproperty

public void  clickOnTheElement(String cssLocator){
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssLocator+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

}

public String getTextOfTheElement(String cssLocator)
{ JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("var x = $(\""+cssLocator+"\");");
        stringBuilder.append("return x.text().toString();")       ;


       String res= (String) js.executeScript(stringBuilder.toString());
       return res;
}

//getElementProperty e.g. color

public String jsGetColor(String css){

        JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x=$(\'"+css+"\');");
        stringBuilder.append("return x.css('color')");
        String res= (String) js.executeScript(stringBuilder.toString());
        return res;

    }

//SO the only thing remains: you have to find css locator of the element you want to interact //with(get property, click on or get text) and pass it to appropriate function:
String myCssLocatorOfTheElement=..blablabla..;
String color=jsGetColor(myCssLocatorOfTheElement);
String text=getTextOfTheElement(myCssLocatorOfTheElement);
 clickOnTheElement(myCssLocatorOfTheElement);

希望这对你有帮助)

于 2012-09-19T10:58:37.517 回答
2

好吧,您的问题来自问题类别,如何从下拉列表中选择一个选项。我通常使用 2 种方法: 方法 1(使用操作构建器,高级用户操作 api):

WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).click();
sbEle = driver.findElement(By.Id("sbEle")).click();

Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).click()

; 你可以在这里获得一些额外的信息

第二种方法(直接使用js):

String cssSelector =..blablabla...
JavascriptExecutor js = (JavascriptExecutor) driver;
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("var x = $(\'"+cssSelector+"\');");
        stringBuilder.append("x.click();");
        js.executeScript(stringBuilder.toString());

希望这对你有用)

于 2012-09-15T14:50:11.880 回答