gpt4 book ai didi

java - Selenium 'Unable to locate element'

转载 作者:行者123 更新时间:2023-12-01 09:31:04 28 4
gpt4 key购买 nike

我知道有几个与此类似的问题。我想我已经经历过它们,但我找不到如何解决我的问题。

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

图标的 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,当点击图标/按钮时,源显示:定位器检测失败

我猜测 HTML 代码中存在某些内容,无法轻松抓取页面。

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

谢谢

最佳答案

试试这个 xpath

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

使用 svg 标签,xpath 失败。

或者CSS

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

关于java - Selenium 'Unable to locate element',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39392562/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com