元素无法使用text-selenium webdriver定位 [英] Element cannot locate with the text- selenium webdriver

查看:159
本文介绍了元素无法使用text-selenium webdriver定位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在研究selenium webdriver并使用Java语言。

Currently working on selenium webdriver and the using the language Java.

Log.info("Clicking on To weekrange dropdown");
JavascriptExecutor executor25 = (JavascriptExecutor)driver;
executor25.executeScript("document.getElementById('toWeekYear).style.display='block';");
Select select25 = new Select(driver.findElement(By.id("toWeekYear")));
select25.selectByVisibleText("2011");
JavascriptExecutor executor26 = (JavascriptExecutor)driver;
executor26.executeScript("document.getElementById('toWeekYear).style.display='block';");
Select select26 = new Select(driver.findElement(By.id(" toWeek")));
select26.selectByVisibleText(" W 5");
Thread.sleep(6000);

在上面的代码中,我试图选择周范围,它在表格中选择年份-2011和周-W 5.这个值是从下拉列表中选择的。问题是在选择第一个下拉列表时选择年份并在尝试选择第二个下拉列表时出现错误无法找到带文本的元素:W 1

In the above code, am trying to select the week range and it is in the form to select year-2011 and week-W 5. This values are selecting from the dropdown. The problem is while selecting 1st dropdown it is selecting the year and while trying to select the second drop down am getting the error Cannot locate element with text: W 1

这是HTML标记:

<select id="fromWeekYear" style="width:60px" name="fromWeekYear">
<option value="2010"> 2010</option>
<option value="2011"> 2011</option>
<option value="2012"> 2012</option>
<option selected="" value="2013"> 2013</option>
</select>
<select id="fromWeek" style="width:60px" name="fromWeek">
<option value="1"> W 1</option>
<option value="2"> W 2</option>
<option value="3"> W 3</option>
<option value="4"> W 4</option>
<option value="5"> W 5</option>

<select id="toWeekYear" style="width:60px" name="toWeekYear">
<option value="2010"> 2010</option>
<option value="2011"> 2011</option>
<option value="2012"> 2012</option>
<option selected="" value="2013"> 2013</option>
</select>
<select id="toWeek" style="width:60px" name="toWeek">
<option value="1"> W 1</option>
<option value="2"> W 2</option>
<option value="3"> W 3</option>
<option value="4"> W 4</option>
<option value="5"> W 5</option>

这是我的fromweek javascript:

Here is my fromweek javascript:

Log.info("Clicking on From weekrange dropdown");
JavascriptExecutor executor23 = (JavascriptExecutor)driver;
     executor23.executeScript("document.getElementById('fromWeekYear').style.display='block';");

Select select23 = new Select(driver.findElement(By.id("fromWeekYear")));
select23.selectByVisibleText("2011");
JavascriptExecutor executor24 = (JavascriptExecutor)driver;
Thread.sleep(6000);
executor24.executeScript("document.getElementById('fromWeek').style.display='block';");
Select select24 = new Select(driver.findElement(By.id("fromWeek")));
select24.selectByVisibleText(" W 1");
Thread.sleep(6000);


推荐答案

试试这个:

new Select(driver.findElement(By.id("fromWeekYear")).selectByValue("2010");
new Select(driver.findElement(By.id("toWeek")).selectByValue("1");

价值是单一的,使用价值。

Value is single, use value.

如果是文字,你的文字有空格:

If by text, your text has space:

new Select(driver.findElement(By.id("fromWeekYear")).selectByVisibleText(" 2010");
new Select(driver.findElement(By.id("toWeek")).selectByVisibleText(" W 1");

其他问题是您的空间可能是(& nbsp;)

Other issue is that maybe your space is (& nbsp;)

这篇关于元素无法使用text-selenium webdriver定位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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