简体   繁体   English

如何使用Selenium单击aria-hidden =“true”的元素

[英]How to click on an element with aria-hidden=“true” using Selenium

I have HTML as below 我有HTML如下

<div class="summary-row">
    <div class="text-center" id="summary-back">
        <a href="/Health/Dependents">
            <svg class="svg-inline--fa fa-chevron-left fa-w-8 font-32" data-auto="back-btn" aria-hidden="true" data-prefix="fal" data-icon="chevron-left" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 512" data-fa-i2svg=""><path fill="currentColor" d="M238.475 475.535l7.071-7.07c4.686-4.686 4.686-12.284 0-16.971L50.053 256 245.546 60.506c4.686-4.686 4.686-12.284 0-16.971l-7.071-7.07c-4.686-4.686-12.284-4.686-16.97 0L10.454 247.515c-4.686 4.686-4.686 12.284 0 16.971l211.051 211.05c4.686 4.686 12.284 4.686 16.97-.001z"></path></svg><!-- <i class="fal fa-chevron-left font-32" data-auto="back-btn"></i> -->
        </a>
        </div>

And RemoteWebDriver unable to find the element and I assume its due to the hidden attribute, How can click on this element ? 并且RemoteWebDriver无法找到该元素,我假设它由于隐藏属性,如何点击这个元素?

You can click on the element using JavaScriptExecutor like: 您可以使用JavaScriptExecutor单击该元素,如:

WebElement element = driver.findElement(By.id("summary-back"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

To click() on the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies : click()你有诱导WebDriverWait元素是可以点击的 ,您可以使用下面的元素定位策略

  • CssSelector : CssSelector

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.summary-row > div#summary-back > a[href=/Health/Dependents]"))).Click(); 
  • XPath : XPath

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='summary-row']/div[@id='summary-back']/a[@href='/Health/Dependents']"))).Click(); 

I just changed attribute "aria-hidden" from "true" to "false" and after that I clicked. 我只是将属性“aria-hidden”从“true”改为“false”,之后我点击了。

Perhaps that code can help you. 也许该代码可以帮助您。

public void ClickElementHidden()
{
((IJavaScriptExecutor)driver).ExecuteScript("document.getElementsByClassName('svg-inline--fa fa-chevron-left fa-w-8 font-32')[0].setAttribute('aria-hidden', 'false')");
driver.FindElement(By.CssSelector("#summary-back > a > svg")).Click();   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM