在我的应用程序中,我想声明一个位于li标记内的webelement名称。但是我找不到相同的位置。
下面是HTML代码的图像。我无法复制粘贴代码,因此会附加图像。
我尝试过的代码是:
IWebElement result = driver.FindElement(By.XPath("//li[@id='licredit-ResultDisplay']/a/b"));
Assert.AreEqual(result.Text, "ESTIMATE RESULTS");
Console.WriteLine("Estimate Result validated successfully");
但是我没有收到这样的元素错误。因此,建议使用任何合适的方法来定位该元素以声明名称-ESTIMATE RESULTS。
您的代码可能会尝试在元素存在之前找到它,因此我的建议是使用wait方法。在Python中,它类似于:
driver = webdriver.Chrome(executable_path=r'D:PATHchromedriver.exe');
driver.get("https://chercher.tech/practice/explicit-wait-sample-selenium-webdriver");
wait = new WebDriverWait(driver, 30 /*timeout in seconds*/);
wait.until(ExpectedConditions.element_to_be_clickable(By.xpath("//button[@id='btn1']"))));
但是我不知道如何在C#中使用它。
也许您正在Xpath中丢失某些字符。这对我有用:
'// * [@ id =“ licredit-ResultDisplay”] / a / b'
由于您不是在寻找标签“ li”,而是在寻找一个由“ id”组成的元素
我希望它能为您工作:)
要摆脱OpenQA.Selenium.NoSuchElementException,您必须为所需的ElementIsVisible()
引入WebDriverWait,并且可以使用以下任何一个Locator Strategies:
使用CssSelector
:
IWebElement result = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("li#licredit-ResultDisplay>a>b")));
Assert.AreEqual(result.Text, "ESTIMATE RESULTS");
Console.WriteLine("Estimate Result validated successfully");
使用XPath
:
IWebElement result = new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//li[@id='licredit-ResultDisplay']/a/b")));
Assert.AreEqual(result.Text, "ESTIMATE RESULTS");
Console.WriteLine("Estimate Result validated successfully");