简体   繁体   English

如何使用 Selenium 和 C# 定位 li 标签内的元素

[英]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.

相关问题 在 <iframe> 通过使用Xpath(Selenium C#) - Locate an HTML element inside of the <iframe> by using Xpath (selenium c#) 如何使用 Selenium WebDriver 和 C# 在 div 中定位按钮 - How to locate a button inside a div with using Selenium WebDriver and C# 如何在C#中使用Selenium在表中查找文本并引用元素单击另一个元素? - How to locate a text in a table and referring the element click on another element using Selenium in C#? 在C#中使用硒无法找到元素错误 - Unable to locate element error using selenium in C# 无法使用 Selenium Chromedriver 在 C# 中定位元素 - Unable to locate element using Selenium Chromedriver when it's there in C# Selenium 如何通过部分 By.Name 定位元素(在 C# 中) - Selenium how to locate element by partial By.Name (in C#) 如何通过Selenium和C#在HTML中定位元素 - How to locate the element within the HTML through Selenium and C# 如何使用 Selenium C# 在 div 和 span 中定位菜单项 - How to locate Menu item inside a div and span with Selenium C# 如何在 Selenium WebDriver 中使用 C# 使用 XPATH 定位动态 Salesforce Lightning INPUT 元素 - How to Locate a DYNAMIC Salesforce Lightning INPUT Element with XPATH using C# in Selenium WebDriver 如何使用 Webdriver 和 C# 通过 Selenium 定位并单击嵌套在多个框架和框架集中的元素 - How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM