Java 如果Web元素是动态的,则无法找到使用Selenium的Web元素
在Selenium中查找web元素时遇到困难html如下所示:Java 如果Web元素是动态的,则无法找到使用Selenium的Web元素,java,selenium,dynamic,selenium-webdriver,Java,Selenium,Dynamic,Selenium Webdriver,在Selenium中查找web元素时遇到困难html如下所示: <div class="d2l-fieldgroup"> <div class=" vui-field-row"> <span class="d2l-field vui-label d2l-offscreen">Do not ask me again for this application</span> <label class="d2l-checkbox-container"
<div class="d2l-fieldgroup">
<div class=" vui-field-row">
<span class="d2l-field vui-label d2l-offscreen">Do not ask me again for this application</span>
<label class="d2l-checkbox-container">
<input name="dontAsk" value="0" type="hidden">
<input id="dontAsk" class="d2l-checkbox vui-input vui-outline" type="checkbox">
Do not ask me again for this application
</label>
</div>
</div>
我试过这个:
WebElement do_not_ask_resourcebank = driver.findElement(By.className("d2l-fieldgroup"));
do_not_ask_resourcebank.click();
但会出现以下错误:
no such element: Unable to locate element: {"method":"class name","selector":"d2l-fieldgroup"}
我还尝试用vui字段行和d2l复选框容器替换d2l字段组
我还尝试:
WebElement do_not_ask_resourcebank = driver.findElement(By.cssSelector("html/body/form/div/div[3]/div/label"));
及
但我似乎无法点击这个元素,这对我这样的新手来说非常令人沮丧。有人能告诉我哪里出了问题吗?如果您得到的异常是
NoTouchElementException
,可能有以下原因:-
- 可能是您使用了不正确的定位器定位,您实际定位的是具有类名的
元素,该类名看起来像动态生成的类名,而不是您想要的实际
元素。而另一个元素看起来也不稳定,您应该尝试使用
as来定位desire元素:-By.id()
WebElement do_not_ask_resourcebank = driver.findElement(By.id("dontAsk"));
- 可能是在您要查找元素时,它不会出现在
上,因此您应该实现DOM
以等待元素显示,如下所示:-WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
- 此元素可能位于任何
或帧
内。如果是,您需要在查找元素之前切换该iframe
或frame
,如下所示:-iframe
WebDriverWait wait = new WebDriverWait(driver, 10); WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));
WebDriverWait wait = new WebDriverWait(driver, 10); //Find frame or iframe and switch wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your frame id or name")); //Now find the element WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk"))); //Once all your stuff done with this frame need to switch back to default driver.switchTo().defaultContent();
我还没有尝试iframe,因为我不确定我在做什么,我没有经常使用这个工具。iframe的html代码是:
如何使用上面的示例定位此iframe
看起来像是iframe
id
和name
都是动态生成的,每次定位时都可能更改,您应该尝试使用唯一的定位器定位此iframe
,我认为它的标题名
是唯一且固定的,因此在定位所需元素之前,您应该尝试定位此iframe
:-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='TAFEPOC']"));
谢谢你的快速回复。你说得对,它不在DOM上。。我添加了:
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS)
并尝试使用:driver.findElement(By.id(“dontAsk”))。单击()不幸的是,它仍然找不到它。我还尝试了:driver.findElement(By.xpath(“/*[@id='confirmForm'])
在使用FirePath检查XPath之后://*[@id='confirmForm']/div/div[3]/div/label
很遗憾,这也不起作用。我会用我的方式完成你剩下的帖子你找frame/iframe了吗??并确保定位的id是唯一的且fixed@tarquin如果不使用隐式等待,请按照建议使用WebDriverWait尝试显式等待,并让我知道..我还没有尝试iframe,因为我不确定我在做什么,我没有经常使用这个工具。iframe的html代码是:''如何使用上面的示例定位此iframe?嗯,这里看起来iframe id和名称都是动态生成的,每次定位时都可能更改,您应该尝试使用唯一定位器定位此iframe,我认为它的标题名是唯一且固定的,所以在定位所需元素之前,您应该尝试定位此iframe,如wait.until(ExpectedConditions.FrameToBeAvailable和SwitchToIt(By.cssSelector(“iframe[title='TAFEPOC']);
,并让我知道