如何使用 Selenium 2 PHPUnit 获取所选选项的文本

How to get the text of the selected option using Selenium 2 PHPUnit

我试图在我的 Selenium 程序中获取文本 "High Gloss Paper Sticker",但我的程序所能做的就是获取所有选项的文本。任何可能帮助我完成此操作的建议或语法将不胜感激。

<select id="attr1" class="op_select" name="attr1">
  
  <option value="16506">Matte Paper Sticker</option>
  <option selected="selected" value="13187">High Gloss Paper Sticker</option>
  <option value="16507">High Gloss Vinyl Sticker</option>

</select>

学习Xpath,你会没事的。

public IWebElement Attr1 {
     get { 
        return this.browser.FindElement(By.XPath(@"//select[@id='attr1']/option[@selected='selected']")
    }
}

id 是查找元素的最快方法。

这只是一个例子 $id_select_box = $this->webDriver->findElement(\WebDriverBy::id('select_box')); $selected_option = new \WebDriverSelect($id_select_box); $this->assertEquals("String", $selected_option->getFirstSelectedOption()->getText());