2016-08-02 53 views
0

我试图选择框架内的下拉值。 This is the code如何使用java在webdriver中选择框架内的下拉值

这就是我们如何写一个链接与框架,但我无法从下拉式

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("right")); 
WebElement el1 = wait.until(ExpectedConditions.elementToBeClickable(By.partialLinkText("Text"))); 
el1.click(); 
+0

图像中帧的名称不是“正确”的,所以它可能没有找到帧。并且与其中的“文本”没有明显的联系,所以这些代码都不应该起作用。你得到的错误是什么? –

回答

-1

值试试下面的代码:

WebElement fr = driver.findElement(By.name("main_b")); 
    driver.switchTo().frame(fr); 
    WebElement dropdown = driver.findElement(By.name("field")); 
    Select sel = new Select(dropdown); 
    sel.selectByValue("<Value to be selected>"); 

您还可以使用等待命令如果你的网页需要一些时间来加载。希望能帮助到你。

1
  • 首先,等待正确的帧(根据HTML代码,该框架的名称是main_b

  • 接下来,你不必有一个链接(<a>标签),所以By.partialLinkText不能使用。使用By.name("field")代替

  • 最后,而不是点击它来得到一个Select对象:Select mySelect = new Select(el1);和使用selectByVisibleTextselectByValueselectByIndex方法

所以一起看起来像这样选择它的一个选项:

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("main_b")); 

Select mySelect = new Select(
    wait.until(
     ExpectedConditions.elementToBeClickable(
      By.name("field") 
))); 

// Select second option by visible text 
mySelect.selectByVisibleText("Bacon Anna"); 

// Same option selected by value 
mySelect.selectByValue("16344"); 

// Same option selected by index 
new Select(el1).selectByIndex(1); 
相关问题