GVKun编程网logo

Selenium Webdriver-在If语句中使用isDisplayed()不起作用(ifwhile语句)

25

在本文中,我们将给您介绍关于SeleniumWebdriver-在If语句中使用isDisplayed的详细内容,并且为您解答不起作用的相关问题,此外,我们还将为您提供关于asp.net–我们可以用I

在本文中,我们将给您介绍关于Selenium Webdriver-在If语句中使用isDisplayed的详细内容,并且为您解答不起作用的相关问题,此外,我们还将为您提供关于asp.net – 我们可以用IIS运行Selenium WebDriver测试用例,而不是Visual Studio开发服务器、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、If 语句在 Selenium Webdriver 中不起作用、java – 为什么拖放在Selenium Webdriver中不起作用?的知识。

本文目录一览:

Selenium Webdriver-在If语句中使用isDisplayed()不起作用(ifwhile语句)

Selenium Webdriver-在If语句中使用isDisplayed()不起作用(ifwhile语句)

我正在创建一个脚本,其中涉及搜索记录,然后更新记录。在搜索屏幕上,用户可以选择查看高级搜索选项。切换显示或隐藏高级搜索是由一个按钮控制的。

<a title="Searches" href="javascript:expandFilters()"><img border="0" align="absmiddle" alt="Advanced" src="****MASKED URL****"></a>

显示或隐藏高级搜索时,搜索按钮的属性之间唯一的区别是img src:

隐藏高级搜索后,IMG src以“ /Styles/_Images/advanced_button.jpg”结尾;当可见高级搜索时,IMG src以“
/Styles/_Images/basic_button.png”结尾

当我打开页面时,有时会显示“高级”搜索选项,有时却没有。我要搜索的值显示在“高级”部分中,因此为了使脚本正常工作,我添加了IF语句。

<input type="text" value="" maxlength="30" size="30" name="guiSystemID">

IF语句查找需要在其中输入数据的字段,如果该字段不存在,则表明“高级”选项不可见,我需要单击按钮以展开搜索选项。

我创建了以下IF语句。

     if (!driver.findElement(By.name("guiSystemID")).isDisplayed()) {         driver.findElement(By.cssSelector("img[alt=''Advanced'']")).click();     }

当我运行脚本并展开“高级”搜索时,脚本将成功运行。但是,当我运行脚本并且未展开“高级”搜索时,脚本失败,建议我找不到对象“
guiSystemID”。令人沮丧的是,如果找不到它,那么我希望脚本继续执行,进入IF语句的True路径。

有没有人对我能如何评估该字段是否出现而不使脚本失败(因为找不到该字段)提出了任何建议。

提前致谢

西蒙

答案1

小编典典

我回答这个问题可能很晚,但是可能会帮助其他人寻找相同的东西。

我最近在使用isDisplayed()时遇到了类似的问题。我的代码是这样的

if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )                                                                                                         {           /**Do this*/     }    else    {       /**Do this*/    }

当存在isDisplayed试图查找的元素时,此代码可以很好地工作。但是当元素不存在时,它将继续寻找该元素,并因此引发异常“
NosuchElementFound”。因此,我无法测试其他部分。

我想出了一种处理此问题的方法(将{if,else}与try and catch块一起使用,这样说。

public void deleteSubVar() throws Exception  {           try     {        if(driver.findElement(By.xpath(noRecordId)).isDisplayed() )         {            /**when the element is found do this*/         }      }        catch(Exception e)       {          /**include the else part here*/       }       }

希望这可以帮助 :)

asp.net – 我们可以用IIS运行Selenium WebDriver测试用例,而不是Visual Studio开发服务器

asp.net – 我们可以用IIS运行Selenium WebDriver测试用例,而不是Visual Studio开发服务器

我正在使用Selenium 2 WebDriver.而不是UnitTest项目,我从网站发起它,原因如下:

>它应该每24小时自动运行一次.我使用System.Threading编写了一些调度代码.
>为客户提供一些UI,以便在需要时中间运行.
>每次运行时,都会发送一封电子邮件作为测试结果的一部分.

我的目标网站是:http://www.vroomvroomvroom.com.au

我创建了一个包含所有Selenium代码的类.我在default.aspx的页面加载时使用System.Threading调用该类.

当我通过按F5或Ctrl F5从Visual Studio运行default.aspx时它工作正常,即使用Visual Studio开发服务器,例如HTTP://本地主机:3251 / Default.aspx的.

但是,当我尝试直接从IIS运行它时,使用默认端口(80),例如http://localhost/seleniumTest/default.aspx,然后失败并出现以下观察/错误:

>它延长了Selenium代码,但没有显示broswer.
>在服务器没有响应的某些步骤后失败了http:// localhost:7094 / hub / session / 4bbe4b0c-aeee-4fa3-8bc0-aae47c6869af / element

这可能是我想要实现的目标.

仅供参考:如果需要进一步的详细信息,请与我们联系.

解决方法

我自己设法找到了解决方案.

基本上,必须使用RemoteWebDriver而不是FirefoxDriver.
脚步:

>将FirefoxDriver的初始化更改为RemoteWebDriver:

改变

IWebDriver driver = new FirefoxDriver();

DesiredCapabilities capability = DesiredCapabilities.Firefox();
Uri url = new Uri("http://REMOTE_IP:4545/wd/hub");
IWebDriver driver = new RemoteWebDriver(url,capability);

2.下载Selenium Standalone服务器并使用〜命令提示符启动它

java -jar E:\Software\selenium-server-standalone-2.24.1.jar -interactive -port 4545

这种方法有两个好处:

>可以使用本地IIS运行测试.
>测试可以远程运行.请参阅Selenium RC文档.人们可以远程使用截图

REMOTE_IP:4545 / WD /集线器/静态/资源/ hub.html

我正在考虑修改其中使用的hub.html和client.js文件的代码,以提供更好的远程感觉.

我希望这对其他人也有用.

供参考:

> IP地址REMOTE_IP可以更改为任何实时IP地址或localhost.在启动页面请求时使用上述端口.>可以在测试内部安装Standalone Server的启动/停止代码,以便通过批处理文件自动启动/停止.>通过不关闭命令提示符来保持服务器运行.

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用

我们有一个.NET 4.5,MVC,C#项目.我们正在使用Selenium进行UI测试,测试会在我们有wait.Until()的行上间歇性地失败.一个这样的例子是:

NoSuchElementException was unhandled by user code
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to locate element: {"method":"css selector","selector":"#assessment-472 .status-PushedToSEAS"}

它就在这里抛出:

Thread.Sleep(600);
wait.Until(drv => drv.FindElement(By.CssSelector("#" + assessmentQueueId + " .status-PushedToSEAS")));

我可以看到浏览器打开,我看到它到达那一点,我可以检查元素以查看元素是否存在.它的id完全正确.

我们有这个问题很多,到目前为止解决方案是在它前面抛出Thread.Sleep(600)(或类似的时间). wait.Until()的重点是不必这样做,这使我们的测试套件变得非常长.另外,正如您在上面的示例中所看到的,有时我们甚至在将Thread.Sleep()放在它前面之后也会遇到问题,我们必须延长时间.

为什么.NET Selenium的WebDriver.Until()不起作用,并且有一种不同的方式来做同样的事情而不需要等待一段时间?同样,问题是间歇性的,这意味着它有时只会发生,它可能发生在任何数量的wait.Until()语句中,而不仅仅是显示的那个!

编辑:

这是一个类变量.
私人WebDriver等等;

它实例化如下:

this.wait = new webdriverwait(driver,TimeSpan.FromSeconds(10));

解决方法

我决定不使用webdriverwait.Until(),而是在主驱动程序上使用隐式等待:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));

为了给我这个想法,我在一个不同的问题上充分赞扬了this answer.

If 语句在 Selenium Webdriver 中不起作用

If 语句在 Selenium Webdriver 中不起作用

如何解决If 语句在 Selenium Webdriver 中不起作用?

我总是对 selenium 中的 if 语句有疑问。大多数时候,他们做的事情与他们应该做的相反。

我故意输入了错误的电子邮件地址,因此付款将失败。你哎呀!显示在网页上,这是正确的响应,但是我的测试返回失败。当我输入正确的电子邮件时,xpath 是正确的。

请有人看一下并告诉我我哪里出错了。

@Test(dependsOnMethods = { "WithdrawalButtonOnNavBar" })
    public void WithdrawWinnings() throws InterruptedException {
        Thread.sleep(500);
        driver.findElement(By.cssSelector("input[name=''email'']")).sendKeys(skrillAccount);
        Thread.sleep(500);
        driver.findElement(By.xpath("//p[contains(text(),''Continue'')]")).click();
        Thread.sleep(500);
        driver.findElement(By.cssSelector("input[name=''amount'']")).sendKeys("2");
        Thread.sleep(500);
        driver.findElement(By.xpath("//*[@id=\"__next\"]/div/div[1]/div[2]/div/div[3]/div/div/div")).click();
        Thread.sleep(5000);
        WebElement Success = driver.findElement(By.xpath("//p[contains(text(),''Success!'')]"));
        WebElement Oops = driver.findElement(By.xpath("//p[contains(text(),''Oops!'')]"));
        if (Success.isdisplayed()) {
            System.out.println("Payment was successful");
        } else if(Oops.isdisplayed())  {
            System.out.println("Payment was unsuccessful");
        }
        ;

    }

org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{"method":"xpath","selector":"//p[contains(text(),''Success!'')] "} (会话信息:chrome=90.0.4430.85) 有关此错误的文档,请访问:https://selenium.dev/exceptions/#no_such_element

解决方法

这与 if 语句无关。查找“成功”元素时抛出异常。显式等待应该会有所帮助:

WebDriverWait wait = new WebDriverWait(driver,30);
WebElement Success = wait.Until(ExpectedConditions.presenceOfAllElementsLocatedBy​(By.xpath("//p[contains(.,''Success!'')]"));

if (Success.isDisplayed())

我还发现 contains(.,''...'')contains(text(),''...'') 更宽容。

java – 为什么拖放在Selenium Webdriver中不起作用?

java – 为什么拖放在Selenium Webdriver中不起作用?

我试图使用Selenium WebDriver将元素拖到另一个元素中,但是它不起作用.我尝试了所有的解决方案,我可以在互联网上找到,但没有一个解决方案似乎在为我工作.
WebElement sourceelement = driver.findElement(By.cssSelector("XXX"));
WebElement destelement = driver.findElement(By.cssSelector("YYY"));

代码1: –

Actions builder = new Actions( _controls.getDriver());
builder.dragAndDrop(sourceelement,destelement);

代码2: –

Actions builder = new Actions(_controls.getDriver());
Action dragAndDrop =
builder.clickAndHold(sourceelement).movetoElement(destelement).release(destelement).build();
Thread.sleep(2000);
dragAndDrop.perform()

CODE3: –

Point coordinates1 = sourceelement.getLocation();
Point coordinates2 = destelement.getLocation();  
Robot robot = new Robot();           
robot.mouseMove(coordinates1.getX(),coordinates1.getY());
robot.mousepress(InputEvent.BUTTON1_MASK);
robot.mouseMove(coordinates2.getX(),coordinates2.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);

码4: –

final String java_script =
"var src=arguments[0],tgt=arguments[1];var dataTransfer={dropEffe" +
                "ct:'',effectAllowed:'all',files:[],items:{},types:[],setData:fun" +
                "ction(format,data){this.items[format]=data;this.types.append(for" +
                "mat);},getData:function(format){return this.items[format];},clea" +
                "rData:function(format){}};var emit=function(event,target){var ev" +
                "t=document.createEvent('Event');evt.initEvent(event,true,false);" +
                "evt.dataTransfer=dataTransfer;target.dispatchEvent(evt);};emit('" +
                "dragstart',src);emit('dragenter',tgt);emit('dragover',tgt);emit(" +
                "'drop',tgt);emit('dragend',src);";

        ((JavascriptExecutor)_controls.getDriver()).executeScript(java_script,sourceelement,destelement);
        Thread.sleep(2000);

以上代码都没有为我工作.所有上述运行没有任何错误,但拖放并没有发生在应用程序中.任何人有任何其他解决方案?谢谢.

解决方法

在你的代码1:
不调用perform()方法,
它应该是

Actions builder = new Actions( _controls.getDriver());

builder.dragAndDrop(sourceelement,destelement).perform();

在你的代码2:我不认为你需要调用release()

请在发布之前搜索similar questions.

关于Selenium Webdriver-在If语句中使用isDisplayed不起作用的问题我们已经讲解完毕,感谢您的阅读,如果还想了解更多关于asp.net – 我们可以用IIS运行Selenium WebDriver测试用例,而不是Visual Studio开发服务器、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、If 语句在 Selenium Webdriver 中不起作用、java – 为什么拖放在Selenium Webdriver中不起作用?等相关内容,可以在本站寻找。

本文标签: