如何通过Selenium和C#定位HTML内的元素
How to locate the element within the HTML through Selenium and C#
我是 c# 新手,在定位元素时遇到问题。我不太明白如何找到相对的 xpath。我正在尝试定位一个元素。我的代码如下:
IWebElement webElement = driver.FindElement(By.XPath("Nothing I put here works"));
Thread.Sleep(1000);
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
这是我要查找的内容:
<li _ngcontent-c2="" class="header-item person-search ng-tns-c2-1 ng-star-inserted" ngbdropdown="" ngbtooltip="Person Search" placement="left">
<a _ngcontent-c2="" class="dropdown-toggle dropdown-toggle" aria-haspopup="true" href="javascript:void(0)" ngbdropdowntoggle="" aria-expanded="false">
<span _ngcontent-c2="" class="fa fa-search-plus block"></span>
</a>
</li>
所需的元素是 Angular element so to locate the element you have to induce WebDriverWait with ExpectedConditions as ElementToBeClickable Method (By),您可以使用以下任一解决方案:
CssSelector
:
IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li.header-item.person-search.ng-star-inserted[ngbtooltip='Person Search']>a.dropdown-toggle>span.fa.fa-search-plus.block"))).Click();
XPath
:
IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//li[@class='header-item person-search ng-tns-c2-1 ng-star-inserted' and @ngbtooltip='Person Search']//a[@class='dropdown-toggle dropdown-toggle']/span[@class='fa fa-search-plus block']")));
更新
如果您正在使用 nuget
,请搜索 DotNetSeleniumExtras.WaitHelpers
并将该命名空间导入您的 class,然后您可以这样做:
new WebDriverWait(driver, new TimeSpan(0, 0, 30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("elementID")));
您可以在
中找到相关讨论
我是 c# 新手,在定位元素时遇到问题。我不太明白如何找到相对的 xpath。我正在尝试定位一个元素。我的代码如下:
IWebElement webElement = driver.FindElement(By.XPath("Nothing I put here works"));
Thread.Sleep(1000);
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
这是我要查找的内容:
<li _ngcontent-c2="" class="header-item person-search ng-tns-c2-1 ng-star-inserted" ngbdropdown="" ngbtooltip="Person Search" placement="left">
<a _ngcontent-c2="" class="dropdown-toggle dropdown-toggle" aria-haspopup="true" href="javascript:void(0)" ngbdropdowntoggle="" aria-expanded="false">
<span _ngcontent-c2="" class="fa fa-search-plus block"></span>
</a>
</li>
所需的元素是 Angular element so to locate the element you have to induce WebDriverWait with ExpectedConditions as ElementToBeClickable Method (By),您可以使用以下任一解决方案:
CssSelector
:IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("li.header-item.person-search.ng-star-inserted[ngbtooltip='Person Search']>a.dropdown-toggle>span.fa.fa-search-plus.block"))).Click();
XPath
:IWebElement webElement = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//li[@class='header-item person-search ng-tns-c2-1 ng-star-inserted' and @ngbtooltip='Person Search']//a[@class='dropdown-toggle dropdown-toggle']/span[@class='fa fa-search-plus block']")));
更新
如果您正在使用 nuget
,请搜索 DotNetSeleniumExtras.WaitHelpers
并将该命名空间导入您的 class,然后您可以这样做:
new WebDriverWait(driver, new TimeSpan(0, 0, 30)).Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(By.Id("elementID")));
您可以在