2013-04-12 120 views
1

我有以下的HTML代码,我需要选择突出显示的元素。如果菜单高亮显示,我想选择菜单中的按钮而不是菜单!如果菜单是赛场,我想选择它。选择按钮的XPath?

<menu id="apl" class="highlight" widgetid="apl"> 
<button id="navi" selectable="true" widgettype="button" widgetid="audio_radio_apl_navi"> 
<button id="audio" class="highlight" selectable="true" widgettype="button" widgetid="audio_radio_apl_audio"> 
<button id="tel" selectable="true" widgettype="button" widgetid="audio_radio_apl_tel"> 
<button id="video" selectable="true" widgettype="button" widgetid="audio_radio_apl_video"> 
<button id="system" selectable="true" widgettype="button" widgetid="audio_radio_apl_system"> 
<button id="net" selectable="true" widgettype="button" widgetid="audio_radio_apl_net"> 
</menu> 
<menu id="plf" widgetid="plf"> 
<img src="img/plf_radio.png"/> 
</menu> 

到目前为止我的代码是:

focusedElement = driver.findElement(By.xpath("//*[contains(@class,'highlight')]")); 

这让我突出显示的菜单,但我需要的按钮! 任何想法?

+0

你能定义一下'如果菜单是赛场,我想选择那'意味着你? – predi

+0

如果我选择一个菜单,然后选择菜单[@ class,'highlight']。但我不想选择菜单元素。我想要突出显示的按钮,这是突出显示的菜单的子项。如果菜单[@ id,'plf']那么我想要菜单,因为它没有按钮。 –

回答

0

如果我正确理解你的问题,这个XPath应该适用于你提供的例子。

//menu[@class='highlight']//button[@class='highlight'] 

它将寻找一个<menu>用含有<button>class='highlight'

+0

出错了:org.openqa.selenium.InvalidSelectorException:给定的选择器// * [contains(@ class,'highlight')]] // button [@ class ='highlight']无效或不会导致一个WebElement。 –

+0

在例外情况下显示的XPath对许多人有一个']'。 – ellak

+0

对不起,在代码中修复仍然没有工作.. –

2

试试这个

focusedElement = driver.findElement(By.xpath("//button[@class='highlight']")); 

简单,因为它可以获取或

Select selectBox = new Select(driver.findElement(By.xpath("//menu[@id='apl']"))); 
0

检查class='highlight'突出显示的元素有i是否改变了。 有时它是由javascript触发的,并创建一个新元素,这是一个很难测试的问题。 还是上面的iFrame提到:d

0

你需要的条件语句: 你应该检查是否元素"//menu[@id='apl']/button[@class='highlight']"存在,如果有,点击它,如果没有,然后点击"//menu[@id='plf']"

0
focusedElement = driver.findElement(By.xpath("//menu[@id='apl']/button[2]"));