硒 'Unable to locate element'

Selenium 'Unable to locate element'

我知道有几个问题与这些问题类似。我想我已经解决了这些问题,但我找不到解决问题的方法。

问题如下。我正在尝试使用 Selenium 驱动程序单击 'https:/unsplash.com/' 网站上的 icon/button,将显示从单网格更改为多网格。

图标的HTML标签如下:

<svg class="N1Ri-" version="1.1" viewBox="0 0 32 32" 
    width="32" height="32" aria-labelledby="icon-title-635 
    icon-desc-636" aria-hidden="false" data-reactid=".pm6nw1xm9s.4.0.0.3.0.0.0.0.1.0">
<path d="M0 2v10c0 1.106 0.896 2 2 
    2h10c1.104 0 2-0.894 2-2v-10c0-1.106-0.896-2-2-2h-10c-1.104 0-2 
    0.894-2 2zM2 18c-1.104 0-2 0.894-2 2v10c0 1.106 
    0.896 2 2 2h10c1.104 0 2-0.894 2-2v-10c0-1.106-0.896-2-2-2h-10zM20 18c-1.106 0-2 
    0.894-2 2v10c0 1.106 0.894 2 2 2h10c1.106 0 2-0.894 2-2v-10c0-1.106-0.
    894-2-2-2h-10zM20 0c-1.106 0-2 0.894-2 2v10c0 1.106 
    0.894 2 2 2h10c1.106 0 2-0.894 2-2v-10c0-1.106-0.894-2-2-2h-10z" 
    data-reactid=".pm6nw1xm9s.4.0.0.3.0.0.0.0.1.0.1">
</path>
</svg>

我的java代码如下:

    public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/home/ xxxx /Documents/Selenium/geckodriver");
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get("https:/unsplash.com/");

    //I've tried all of the following independently with the same result: 'Unable to locate element'
    driver.findElement(By.className("N1Ri")).click();
    driver.findElement(By.className("N1Ri-")).click();
    driver.findElement(By.className("svg.Hd1sr")).click();
    driver.findElement(By.xpath("//a[contains(@class,'N1Ri')]")).click();
    driver.findElement(By.xpath("//*[@id='svg.Hd1sr']")).click();
}

正如您将在代码中看到的那样,我尝试了一系列选项(我尝试了许多其他组合)但总是得到相同的结果:无法定位元素

我也尝试了 Senenium IDE,当在 icon/button 上叮当作响时,源显示:LOCATOR DETECTION FAILED

我猜 HTML 代码中有一些东西可以防止轻易抓取页面。

我的问题有两个: - 在这种情况下我必须使用的代码是什么? -(最重要的)我如何找到在这种情况下使用的代码?

谢谢

尝试:

driver.findElement(By.cssSelector("svg.N1Ri-")).click();

出于某种原因,与此等效的 xpath 不起作用,恐怕我不知道为什么。

试试这个 xpath

("//a[contains(@href,'multi')]/*[@class='Hd1sr']")

带有 svg 标签的 xpath 失败。

或css

"a[href*='multi']>svg[class='Hd1sr']"