2013-12-09 83 views
2

目前正在使用硒webdriver和使用Java語言。元素無法找到與文本 - 硒webdriver

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

這裏是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> 

這裏是我的javascript fromweek:

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); 
+0

的樣子,在加載第二個下拉(有些延時基於第一個下拉選擇)。在嘗試選擇第二個下拉菜單之前,請先介紹約2秒的等待時間。 – Surya

回答

2

試試這個:

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

值是單一的,實用價值。

如果文本,文本具有空間:

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

另一個問題是,也許你的空間(& NBSP;)

+0

謝謝,一旦我現在刪除空間它工作正常 – Amirdha

1

你有一些距離的問題:

driver.findElement(By.id(" toWeek")));

不應該有id的空間:

driver.findElement(By.id("toWeek")));

+0

謝謝。但是我從週中得到問題本身 – Amirdha

+0

你可以發佈訪問'fromWeek'的Javascript嗎? –

+0

任何人都可以給我這個問題的解決方案 – Amirdha