在seleniumn中查找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">
<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>我需要点击复选框或文本,但我似乎无法做到这一点,因为我总是收到错误回复说:
Unable to locate element: {"method":"class name","selector"我试过这个:
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-field-row和d2l-复选框-容器替换d2l-field组。
我也试过:
WebElement do_not_ask_resourcebank = driver.findElement(By.cssSelector("html/body/form/div/div[3]/div/label"));和
WebElement do_not_ask_resourcebank = driver.findElement(By.xpath("id('confirmForm')/x:div/x:div[3]/x:div/x:label"));但我似乎不能点击这个元素,对像我这样的新手来说非常令人沮丧。有人能帮我指出我哪里出错了吗?
发布于 2016-10-06 01:08:00
如果您获得NoSuchElementException作为您提供的例外,可能有以下原因:
<div>元素,它看起来像动态生成的类名,而不是您想要的实际<input type = 'checkbox'>元素。另外一些看起来也不稳定,您应该尝试使用By.id()来定位欲望元素,如:
WebElement do_not_ask_resourcebank = driver.findElement(By.id("dontAsk"));DOM上,因此您应该实现WebDriverWait以等待元素可见,如下所示:-
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));WebDriverWait WebDriverWait(驱动程序,10);WebElement el =WebDriverWaitframe或iframe中。如果是的话,您需要在找到元素之前切换frame或iframe,如下所示:-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("your wait = need ( WebDriverWait,10);// frame或iframe并切换wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dontAsk")));框架id或名称“);//现在查找元素WebElement el =wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(”dontAsk“)));//,一旦完成此框架的所有操作都需要切换回默认的driver.switchTo().defaultContent();编辑的:
我还没有尝试过iframe,因为我不知道我在做什么,我没有经常使用这个工具。iframe的html代码是:
<iframe id="d2l_1_69_954" class="d2l-iframe d2l-iframe-fullscreen d2l_1_67_746" name="d2l_1_69_954" src="/d2l/lms/remoteplugins/lti/launchLti.d2l?ou=6606&pluginId=f8acc58b-c6d5-42df-99d9-e334f825c011&d2l_body_type=3" title="TALLPOD" scrolling="auto" allowfullscreen="" onload="this.m_loaded=true;" style="height: 314px;" frameborder="0">,如何使用上面的示例找到这个iframe?
看起来iframe id和name都是动态生成的,每次定位时都可能发生更改,您应该尝试使用唯一的定位器来定位这个iframe,我认为title name是唯一的和固定的,所以在定位所需的元素之前,应该尝试定位这个iframe :-
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[title='TAFEPOC']"));https://stackoverflow.com/questions/39885467
复制相似问题