如何使用Selenium WebDriver在嵌套范围中查找元素? [英] How to find element in nested span using Selenium WebDriver?

查看:553
本文介绍了如何使用Selenium WebDriver在嵌套范围中查找元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图单击范围n6中的新贸易"链接,该链接是范围n2的子元素.我可以到达n2,但无法识别n6. 请帮助我是Selenium WebDriver的新手

I am trying to click on 'New Trade' link which is in span n6 which is a child element of span n2. I am able to reach till n2 but its not identifying 'n6'. Please help I am new to Selenium WebDriver

我在这里发布html和我的代码.

Here I am posting the html and my code.

交易

Trading 

新 交易

New Trade 

贸易 资源管理器

Trade Explorer 

我要点击新交易

HTML源代码

我的代码一直持续到'n2':

My code which went till span 'n2':

driver.switchTo().frame(driver.findElement(By.name("treeFrame")));

WebElement allFormChildElements = driver.findElement(By.name("the_form"));
allFormChildElements.findElement(By.linkText("Trading")).click();  

WebElement modalDialog = allFormChildElements.findElement(By.className("border"));
WebElement newmodalDialog = modalDialog.findElement(By.className("formScrollableMenuContent")); 
System.out.println(newmodalDialog.findElements(By.tagName("a")).size()); // ans 5 

WebElement newDialog= newmodalDialog.findElement(By.id("n2"));
System.out.println(newDialog.findElements(By.id("n3")).size()); // ans 0

推荐答案

对于我无法测试的图像,但是,如果您能够到达元素<span id="n2">,则可以从此处使用以下xpath以便单击带有文本"New Trade"的元素:

With the image I can't test but, if you are able to reach the element <span id="n2">, from there you could use the following xpath in order to click the element with the text "New Trade":

newDialog.findElement(By.xpath(".//span[@id='n6']/a[@name='A6' and text()='New Trade']")).click();

编辑

如果id值更改,请尝试以下方式:

If the id value change, try in this way:

newDialog.findElement(By.xpath(".//span/a[@name='A6' and text()='New Trade']")).click();

这篇关于如何使用Selenium WebDriver在嵌套范围中查找元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆