[英]Selenium c# Find element exist on a page using Pagefactory
I am migrating my selenium scripts to PageFactory and currently stuck with find if an element exist on the page 我正在将硒脚本迁移到PageFactory,并且目前一直在查找页面上是否存在元素
My current implementation is 我当前的实现是
public bool IsElementExist(string element)
{
try
{
Driver.FindElement(By.XPath(element));
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
My page has multiple items on its basket and if I need to delete all/some of the item. 我的页面上的购物篮中有多个项目,如果我需要删除所有/某些项目。 Everytime I delete the page refreshes, but the Pagefactory FindsBy is not getting refreshed and retains the very first value.
每次删除页面时,页面都会刷新,但是Pagefactory FindsBy不会刷新,并且会保留第一个值。 Any assistance please.
请协助。
When you have multiple elements matching a given locator, the findElement() method simply picks the first one and returns the corresponding WebElement. 当有多个元素与给定的定位器匹配时,findElement()方法仅选择第一个并返回相应的WebElement。 This could be confusing sometimes, because you might interact with a different element unintentionally.
有时这可能会造成混淆,因为您可能会无意间与其他元素进行交互。
A slightly better implementation of your function would be as below (example in Java but works similarly in C#): 下面是一个更好的函数实现方式(Java中的示例,但在C#中的工作方式类似):
public Boolean objectExists(By by) {
int numberOfMatches = driver.findElements(by).size();
if(numberOfMatches == 1) {
return true;
}
else {
// 0 matches OR more than 1 match
return false;
}
}
Not sure if this is directly related to your issue, but worth a try. 不知道这是否与您的问题直接相关,但是值得一试。
Below I am performing a wait but it provides an example of how you can pass an IWebElement to find an object and perform an action. 在下面,我正在执行等待,但是它提供了一个示例,该示例说明如何传递IWebElement来查找对象并执行操作。 The method you provided is going to expect you to provide the XPath as a string rather than the name of an already identified element on the page.
您提供的方法将期望您以字符串形式提供XPath,而不是页面上已经标识的元素的名称。
// Wait Until Object is Clickable
public static void WaitUntilClickable(IWebElement elementLocator, int timeout)
{
try
{
WebDriverWait waitForElement = new WebDriverWait(DriverUtil.driver, TimeSpan.FromSeconds(10));
waitForElement.Until(ExpectedConditions.ElementToBeClickable(elementLocator));
}
catch (NoSuchElementException)
{
Console.WriteLine("Element with locator: '" + elementLocator + "' was not found in current context page.");
throw;
}
}
This lives in a BaseUtil.cs that I call from MyPageFunctionality.cs like this: 这位于我从MyPageFunctionality.cs调用的BaseUtil.cs中,如下所示:
BaseUtil.WaitUntilClickable(btnLogin, 10);
Would this work? 这行得通吗?
//call the method and pass the xpath. You can also create css, id etc.
page.WaitForElementNoLongerDisplayed_byXpath("//div[contains(@class, 'my-error-message')]");
public static void WaitForElementNoLongerDisplayed_byXpath(string elementXpath)
{
try
{
_wait.Until(driver => driver.FindElements(By.XPath(elementXpath)).Count == 0);
}
catch (Exception)
{
LogFunctions.WriteError("Element is still displayed and should not be");
TakeScreenshot("elementStillShown");
throw;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.