Selenium C# 标签文本点击
Selenium C# label text click
我有这个元素:
<label _ngcontent-c12="" tabindex="0" class="">
<input _ngcontent-c12="" type="radio" class="ng-untouched ng-valid ng-dirty">
<span _ngcontent-c12=""></span> TEST
</label>
以及如何使用 selenium C# 单击它?我试试:
driver.FindElement(By.XPath("//label[text()='TEST']")).Click();
但是没用
所需的元素是 Angular element so you have to induce WebDriverWait for the desired ElementToBeClickable()
and you can use either of the following 作为解决方案:
XPath 1:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']")));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[contains(., 'TEST')]//input"))).Click();
XPath 2:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']")));
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[normalize-space()='TEST']//input"))).Click();
可能是因为'TEST'之前少了space。您可以尝试 driver.FindElement(By.XPath("//label[contains(text(),'TEST')]")).Click();
或
driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();
.
此外,原因可能是没有 WebDriverWait
我觉得你可以做得更简单:
driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();
driver.FindElement(By.CssSelector(".ng-untouched.ng-valid.ng-dirty")).Click();
我在这里试图找到需要点击的特定文本。
IWebDriver driver = new ChromeDriver();
var label = driver.FindElements(by.TagName("span")).FirstOrDefault(ele=>ele.Text.Equals("TEST"));
label.Click();
如果上述解决方案不起作用,但在大多数情况下,在 Span 标签内,显示文本 == false 那么我们需要使用 Actions Class.
Actions action = new Actions(driver);
action.MoveToElement(label).Click().Perform();
我有这个元素:
<label _ngcontent-c12="" tabindex="0" class="">
<input _ngcontent-c12="" type="radio" class="ng-untouched ng-valid ng-dirty">
<span _ngcontent-c12=""></span> TEST
</label>
以及如何使用 selenium C# 单击它?我试试:
driver.FindElement(By.XPath("//label[text()='TEST']")).Click();
但是没用
所需的元素是 Angular element so you have to induce WebDriverWait for the desired ElementToBeClickable()
and you can use either of the following
XPath 1:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']"))); new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[contains(., 'TEST')]//input"))).Click();
XPath 2:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.InvisibilityOfElementLocated(By.XPath("//div[@class='wrapper']"))); new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//label[normalize-space()='TEST']//input"))).Click();
可能是因为'TEST'之前少了space。您可以尝试 driver.FindElement(By.XPath("//label[contains(text(),'TEST')]")).Click();
或
driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();
.
此外,原因可能是没有 WebDriverWait
我觉得你可以做得更简单:
driver.FindElement(By.XPath("//label[contains(.,'TEST')]")).Click();
driver.FindElement(By.CssSelector(".ng-untouched.ng-valid.ng-dirty")).Click();
我在这里试图找到需要点击的特定文本。
IWebDriver driver = new ChromeDriver();
var label = driver.FindElements(by.TagName("span")).FirstOrDefault(ele=>ele.Text.Equals("TEST"));
label.Click();
如果上述解决方案不起作用,但在大多数情况下,在 Span 标签内,显示文本 == false 那么我们需要使用 Actions Class.
Actions action = new Actions(driver);
action.MoveToElement(label).Click().Perform();