[英]How to locate the element inside li tag using Selenium and C#
In my application, I want to assert a webelement name which is inside li tag.在我的应用程序中,我想声明一个位于 li 标签内的 webelement 名称。 But I am unable to locate the same.
但我无法找到相同的位置。
Below is the image of HTML code.I am unable to copy paste the code hence attaching the image.下面是 HTML 代码的图像。我无法复制粘贴代码,因此附上图像。
The code which i tried is:我尝试的代码是:
IWebElement result = driver.FindElement(By.XPath("//li[@id='licredit-ResultDisplay']/a/b"));
Assert.AreEqual(result.Text, "ESTIMATE RESULTS");
Console.WriteLine("Estimate Result validated successfully");
But i am getting no such element error.So kindly suggest any suitable way to locate the element to assert the name-ESTIMATE RESULTS.但是我没有收到这样的元素错误。所以请建议任何合适的方法来定位元素以断言名称-ESTIMATE RESULTS。
your code may try to find the element before it exists, so my suggestion is use wait method.您的代码可能会在元素存在之前尝试找到它,所以我的建议是使用等待方法。 In Python it is something like:
在 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']"))));
But I don't know how to use it in C#.但是我不知道如何在C#中使用它。
Maybe you are losing some character inside the Xpath.也许您在 Xpath 中失去了一些特性。 This works for me:
这对我有用:
'//*[@id="licredit-ResultDisplay"]/a/b'
'//*[@id="licredit-ResultDisplay"]/a/b'
Since you are not looking for a tag "li" but an element by "id"因为您不是在寻找标签“li”,而是在寻找“id”的元素
I hope it can work for you:)我希望它可以为你工作:)
To get rid of OpenQA.Selenium.NoSuchElementException you have to induce WebDriverWait for the desired ElementIsVisible()
and you can use either of the following Locator Strategies :要摆脱OpenQA.Selenium.NoSuchElementException您必须为所需的
ElementIsVisible()
诱导WebDriverWait并且您可以使用以下任一定位器策略:
Using CssSelector
:使用
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");
Using XPath
:使用
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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.