本文将介绍seleniumwaitForElement的详细情况,。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于C#Appium/SeleniumDeriv
本文将介绍seleniumwaitForElement的详细情况,。我们将通过案例分析、数据研究等多种方式,帮助您更全面地了解这个主题,同时也将涉及一些关于C# Appium/Selenium Derive WindowsElement 抛出错误、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、c# – Selenium Wait不等待Element可点击、FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException的知识。
本文目录一览:- seleniumwaitForElement
- C# Appium/Selenium Derive WindowsElement 抛出错误
- c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用
- c# – Selenium Wait不等待Element可点击
- FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException
seleniumwaitForElement
我如何编写Selenium函数来等待Python中只有类标识符的表?我在学习使用Selenium的Python Webdriver函数时遇到了恶魔。
答案1
小编典典从seleniumhq文档PDF:
import contextlibimport selenium.webdriver as webdriverimport selenium.webdriver.support.ui as uiwith contextlib.closing(webdriver.Firefox()) as driver: driver.get(''http://www.google.com'') wait = ui.WebDriverWait(driver,10) # Do not call `implicitly_wait` if using `WebDriverWait`. # It magnifies the timeout. # driver.implicitly_wait(10) inputElement=driver.find_element_by_name(''q'') inputElement.send_keys(''Cheese!'') inputElement.submit() print(driver.title) wait.until(lambda driver: driver.title.lower().startswith(''cheese!'')) print(driver.title) # This raises # selenium.common.exceptions.TimeoutException: Message: None # after 10 seconds wait.until(lambda driver: driver.find_element_by_id(''someId'')) print(driver.title)
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# – .NET Selenium NoSuchElementException; WebDriverWait.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));
解决方法
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
为了给我这个想法,我在一个不同的问题上充分赞扬了this answer.
c# – Selenium Wait不等待Element可点击
webdriverwait wait = new webdriverwait(Driver.Instance,TimeSpan.FromSeconds(30)); wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("addinspectionButton"))); var button = Driver.Instance.FindElement(By.Id("addinspectionButton")); button.Click();
这不起作用. click事件永远不会被触发. selenium脚本不会抛出异常,提示ID为“addinspectionButton”的元素不存在.它只是无法点击它.如果我添加一个Thread.Sleep(3000)在wait语句和我得到按钮元素句柄的行之间它可以工作.
我在这里没有正确使用ExpectedConditions.ElementToBeClickable吗?
解决方法
我的解决方案是单击按钮,检查预期结果,然后重复,如果预期结果尚未在DOM中.
由于预期的结果是打开一个表单,我像这样轮询DOM:
button.Click();//click button to make form open var forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//query the DOM for the form var times = 0;//keep tabs on how many times button has been clicked while(forms.Count < 1 && times < 100)//if the form hasn't loaded yet reclick the button and check for the form in the DOM,only try 100 times { button.Click();//reclick the button forms = Driver.Instance.FindElements(By.Id("inspectionDetailsForm"));//requery the DOM for the form times++;// keep track of times clicked }
FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException
我正在尝试开始编写Selenium测试,并且我编写的第一个非常基本的测试因exception失败OpenQA.Selenium.DriverServiceNotFoundException
。
using OpenQA.Selenium;using OpenQA.Selenium.Firefox;namespace WebDriverDemo{ class Program { static void Main(string[] args) { IWebDriver driver = new FirefoxDriver(); driver.Url = "http://www.google.com"; } }}
调试器说我需要下载geckodriver.exe并将其设置在我的PATH变量上,这已经完成,但仍然会出现相同的异常。当我对进行相同的操作时ChromeDriver
,效果很好。
同样,根据MDN,如果我使用的是Selenium
3.0或更高版本,则应默认启用它。我在Windows 10计算机上使用Selenium 3.0.1。
答案1
小编典典您可以使用System.setProperty()方法将geckodriver位置添加到PATH中,或仅将其添加到代码中。
看看下面的链接。它显示了Java中的基本代码,您需要编写等效的C#。但是它提到了使用GeckoDriver的步骤。如果仍然无法使用,请分享您得到的错误。
http://www.automationtestinghub.com/selenium-3-0-launch-firefox-with-
geckodriver/
关于seleniumwaitForElement的介绍已经告一段落,感谢您的耐心阅读,如果想了解更多关于C# Appium/Selenium Derive WindowsElement 抛出错误、c# – .NET Selenium NoSuchElementException; WebDriverWait.Until()不起作用、c# – Selenium Wait不等待Element可点击、FireFox中的Selenium OpenQA.Selenium.DriverServiceNotFoundException的相关信息,请在本站寻找。
本文标签: