在这篇文章中,我们将带领您了解调用FindElements后,selenium停止工作的全貌,包括seleniumfindelement的相关情况。同时,我们还将为您介绍有关C#Appium/Sele
在这篇文章中,我们将带领您了解调用FindElements后,selenium停止工作的全貌,包括selenium find element的相关情况。同时,我们还将为您介绍有关C# Appium/Selenium Derive WindowsElement 抛出错误、c# – 是否可以在不安装Selenium Server的情况下使用ISelenium / DefaultSelenium?、findElement 的 Selenium xpath 语法错误、Handle AJAX elements in Selenium 2 (WebDriver)的知识,以帮助您更好地理解这个主题。
本文目录一览:- 调用FindElements后,selenium停止工作(selenium find element)
- C# Appium/Selenium Derive WindowsElement 抛出错误
- c# – 是否可以在不安装Selenium Server的情况下使用ISelenium / DefaultSelenium?
- findElement 的 Selenium xpath 语法错误
- Handle AJAX elements in Selenium 2 (WebDriver)
调用FindElements后,selenium停止工作(selenium find element)
有时,当我调用Selenium FindElements(By)时,它将引发异常,并且驱动程序停止工作。参数“
BY”可能是问题所在:当我使用其他BY搜索相同的元素时,它起作用了。
我也可以看到,即使我的元素存在,或者之前曾调用过带有相同参数的相同方法,也不会阻止该方法引发异常。
我的方法是:
public IWebElement SafeFindElement(By by) { try { IWebElement element; if (_driver.FindElements(by).Any()) { element = _driver.FindElements(by).First(); return element; } return null; } catch (NoSuchElementException) { return null; } catch (Exception) { return null; } }
一个BY值的示例并非始终有效(即使它存在于页面中):
By.CssSelector("input[data-id-selenium=''entrar'']")
例外:
WebDriverException
到远程WebDriver服务器的URL http:// localhost:46432 / session /
ef6cd2f1bf3ed5c924fe29d0f2c677cf /
elements的HTTP请求
在60秒后超时。
我不知道这可能是什么或导致这种不稳定的原因。有人建议吗?
@编辑
我找到了一个临时解决方案。
早期,我尝试使用以下方法查找元素:
var element = browser .FindElements(By.CssSelector("input[data-id-selenium=''entrar'']") .FirstOrDefault();
要么
var element = browser .FindElements(By.XPath("//input[@data-id-selenium=''entrar'']"); .FirstOrDefault();
现在,我正在使用:
var element = browser .FindElements(By.TagName("input")) .FirstOrDefault(x => x.GetAttribute("data-id-selenium") == "entrar");
他们做同样的事情,但是首先会无故抛出异常。另外,这是一个临时解决方案,我正在尝试解决仅使用选择器搜索元素的问题。
答案1
小编典典我发现了问题。我在所有测试服中都使用一种方法来等待加载消息关闭。但是它尝试使用jquery,但并非我的应用程序中的所有页面都使用它。
因此,Selenium放弃尝试在60秒后执行jquery并返回超时错误,但是此错误不会破坏Selenium驱动程序,只有FindElements才返回空列表。当它尝试返回空列表时,所有驱动器都坏了。
原始方法:
public void WaitLoadingMessage(int timeout){ while (timeout > 0) { try { var loadingIsVisible = _js.ExecuteScript("return $(''#loading-geral'').is('':visible'');").ToString(); if (loadingIsVisible.ToLower() == "false") break; Thread.Sleep(1000); timeout -= 1000; } catch (Exception ex) { if (!ex.Message.ToLower().Contains("$ is not defined")) throw; } }}
和更正:
public void WaitLoadingMessage(int timeout){ while (timeout > 0) { try { var loadingIsVisible = _js.ExecuteScript("return $(''#loading-geral'').is('':visible'');").ToString(); if (loadingIsVisible.ToLower() == "false") break; Thread.Sleep(1000); timeout -= 1000; } catch (Exception ex) { if (!ex.Message.ToLower().Contains("$ is not defined")) throw; break; } }}
C# Appium/Selenium Derive WindowsElement 抛出错误
如何解决C# Appium/Selenium Derive WindowsElement 抛出错误?
在我的 C# .NET Framework 4.8 项目中,我试图派生 WindowsElement 类以在新类中实现一些附加功能。 例如 ClickInMiddleOfControl、IsVisible 或 SelectedItem(在组合框中)。
public class WindowsElementWrapper : WindowsElement
{
public WindowsElementWrapper(RemoteWebDriver parent,string id) : base(parent,id) { }
public void ClickInMiddleOfControl()
{
var rect = this.Rect;
var offsetx = (int)(0.5 * rect.Width);
var offsety = (int)(0.5 * rect.Height);
new Actions(Helper.Session)
.MovetoElement(this,0)
.MoveByOffset(offsetx,offsety)
.Click()
.Build()
.Perform();
}
}
然后我尝试将 WindowsElement 转换为我的 WindowsElementWrapper。
var element = (WindowsElementWrapper)Session.FindElementByName("ElementName");
element.ClickInMiddleOfControl();
但在运行时我收到以下错误消息:
system.invalidCastException: ''无法转换类型的对象 OpenQA.Selenium.Appium.Windows.WindowsElement'' 输入 ''WEW.WindowsElementWrapper''。''
是否不能从 WindowsElement 类派生?还是我犯了一个根本性的错误?
解决方法
从 WindowsElement
到 WindowsElementWrapper
的转换是沮丧。
为了使向下转型成功,对象的运行时类型必须是目标类型本身,或者是从它派生的类型。
虽然 WindowsElementWrapper
是 WindowsElement
,但 WindowsElement
不一定是 WindowsElementWrapper
。
换句话说,这种向下转换永远不会成功,除非 SeleniumAPI 实例化对象作为一个 WindowsElementWrapper
开始。
为了实现您想要做的事情,您可以应用以下设计之一:
组成
public class WindowsElementWrapper
{
private readonly WindowsElement element;
public WindowsElementWrapper(WindowsElement element,(int Width,int Height) rect)
{
this.element = element;
Rect = rect;
}
public (int Width,int Height) Rect { get; init; }
public void ClickInMiddleOfControl()
=> new Actions(Helper.Session)
.MoveToElement(element,0)
.MoveByOffset(Rect.Width / 2,Rect.Height / 2)
.Click()
.Build()
.Perform();
}
扩展方法
public static class Extensions
{
public static void ClickInMiddleOfControl(this WindowsElement source,int Height) rect)
=> new Actions(Helper.Session)
.MoveToElement(source,0)
.MoveByOffset(rect.Width / 2,rect.Height / 2)
.Click()
.Build()
.Perform();
}
最好的问候
c# – 是否可以在不安装Selenium Server的情况下使用ISelenium / DefaultSelenium?
这是DefaultSelenium的构造函数:
ISelenium sele = new DefaultSelenium(**serveraddr**,**serverport**,browser,url2test); sele.Start(); sele.open(); ...
似乎我必须在创建ISelenium对象之前安装Selenium Server.
我的情况是,我正在尝试使用C#Selenium构建一个.exe应用程序,它可以在不同的PC上运行,并且不可能在所有PC上安装Selenium Server(你永远不知道哪个是下一个运行应用程序).
有没有人知道如何在不安装服务器的情况下使用ISelenium / DefaultSelenium?
谢谢!
解决方法
1)对于selenium浏览器启动:
DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setbrowserName("safari"); CommandExecutor executor = new SeleneseCommandExecutor(new URL("http://localhost:4444/"),new URL("http://www.google.com/"),capabilities); WebDriver driver = new RemoteWebDriver(executor,capabilities);
2)对于selenium命令:
// You may use any WebDriver implementation. Firefox is used here as an example WebDriver driver = new FirefoxDriver(); // A "base url",used by selenium to resolve relative URLs String baseUrl = "http://www.google.com"; // Create the Selenium implementation Selenium selenium = new WebDriverBackedSelenium(driver,baseUrl); // Perform actions with selenium selenium.open("http://www.google.com"); selenium.type("name=q","cheese"); selenium.click("name=btnG"); // Get the underlying WebDriver implementation back. This will refer to the // same WebDriver instance as the "driver" variable above. WebDriver driverInstance = ((WebDriverBackedSelenium) selenium).getWrappedDriver(); //Finally,close the browser. Call stop on the WebDriverBackedSelenium instance //instead of calling driver.quit(). Otherwise,the JVM will continue running after //the browser has been closed. selenium.stop();
描述于此:http://seleniumhq.org/docs/03_webdriver.html
谷歌在C#中有类似的东西.没有其他方法可以实现这一目标.
findElement 的 Selenium xpath 语法错误
如何解决findElement 的 Selenium xpath 语法错误?
我刚刚开始使用 Selenium 进行自动化测试,并且一直在通过 udemy 学习教程。
一旦用户登录,我就找到了导航栏的 xpath,以确保该人已登录。在控制台中对其进行了测试,它可以找到导航栏。在我的脚本中设置它,它返回一个语法错误。
这是我试图定位的导航栏的 html:
<ui-viewhttps://www.jb51.cc/tag/cop/" target="_blank">cope">
<app-route >
<div>
<app-headerhttps://www.jb51.cc/tag/cop/" target="_blank">cope">
<header>
<nav>
我的 xpath 代码:
WebElement navBar = driver.findElement(By.xpath("//ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar'']"));
错误:
[ERROR] Tests run: 1,Failures: 1,Errors: 0,Skipped: 0,Time elapsed: 13.796 s <<< FAILURE! - in com.testing.autoTest
[ERROR] com.testing.autoTest.loginTest Time elapsed: 8.387 s <<< FAILURE!
org.openqa.selenium.InvalidSelectorException:
invalid selector: Unable to locate an element with the xpath expression //ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar''] because of the following error:
SyntaxError: Failed to execute ''evaluate'' on ''Document'': The string ''//ui-view[@ng-scope'']/app-route/div[@AppWrapper]/app-header[@ng-isolate-scope'']//nav[@NavBar'']'' is not a valid XPath expression.
控制台测试:
$x("//ui-view[@ng-scope'']/app-route/div[@AppWrapper'']/app-header[@ng-isolate-scope'']//nav[@NavBar'']")
[nav.NavBar]0: nav.NavBarlength: 1__proto__: Array(0)
我已将 xpath 分解为以 //ui-view[@ng-scope'']
开头的增量块,并逐渐向其中添加更多块,似乎一旦到达 //ui-view[@ng-scope'']/app-route/div[@AppWrapper]
,测试就会失败。
我也尝试过在页面上使用其他元素,但它在同一点中断。
如有任何提示,将不胜感激,谢谢。
编辑: zx485 指出的错误导致新错误:
[ERROR] com.testing.autoTest.loginTest Time elapsed: 9.271 s <<< FAILURE!
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"xpath","selector":"//ui-view[@ng-scope'']/app-route/div[@AppWrapper'']/app-header[@ng-isolate-scope'']//nav[@NavBar'']"}
编辑 2:我发现了第二个错误的问题,测试试图在元素加载之前运行得太快,因此在查找导航栏之前添加了一个 sleep(3000);
计时器并且测试通过了
解决方法
如评论中所述,您有一个类型 - 在 ''
中缺少结束 div[@AppWrapper]
。
但是我建议使用这个定位器:
//nav[@NavBar'']
这应该足够独特。
Handle AJAX elements in Selenium 2 (WebDriver)
总结
以上是小编为你收集整理的Handle AJAX elements in Selenium 2 (WebDriver)全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
今天关于调用FindElements后,selenium停止工作和selenium find element的分享就到这里,希望大家有所收获,若想了解更多关于C# Appium/Selenium Derive WindowsElement 抛出错误、c# – 是否可以在不安装Selenium Server的情况下使用ISelenium / DefaultSelenium?、findElement 的 Selenium xpath 语法错误、Handle AJAX elements in Selenium 2 (WebDriver)等相关知识,可以在本站进行查询。
本文标签: