gpt4 book ai didi

java - 元素无法使用 text-selenium webdriver 定位

转载 作者:行者123 更新时间:2023-11-29 18:19:11 25 4
gpt4 key购买 nike

目前正在研究 selenium webdriver 和使用 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);

在上面的代码中,我试图选择周范围,它是在表格中选择 year-2011 和 week-W 5。这些值是从下拉列表中选择的。问题是在选择第一个下拉菜单时选择的是年份,而在尝试选择第二个下拉菜单时出现错误 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:

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)单一,使用值(value)。

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

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

另一个问题是,也许你的空间是( )

关于java - 元素无法使用 text-selenium webdriver 定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20462732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com