2017-02-16 8 views
1

この場合、選択したオプション名:Option3を取得したいので問題があります。この場合、値が正しく選択されているかどうかを確認するためにassertを使用したいと思います。あなたは以下の私のページの一部を見ることができます:選択したオプションを取得する方法(Selenium&Python)

<html> 
 
\t <body> 
 
\t \t <table border="0" cellpadding="0" cellspacing="0" class="rich-toolbar " id="mainMenuToolbar" width="100%"> 
 
\t \t \t <tbody> 
 
\t \t \t \t <tr valign="middle"> 
 
\t \t \t \t \t <td class="rich-toolbar-item " style=";"> 
 
\t \t \t \t \t \t <form id="substituteForm" name="name" method="post" action="http://homepage/home.seam" enctype="application/x-www-form-urlencoded"> 
 
\t \t \t \t \t \t \t <select name="substituteForm:j_id158" size="1" onchange="document.getElementById(&#39;substituteForm:substituteSubmit&#39;).click();"> 
 
\t \t \t \t \t \t \t \t <option value="0">Option0</option> 
 
\t \t \t \t \t \t \t \t <option value="1">Option2</option> 
 
\t \t \t \t \t \t \t \t <option value="2" selected="selected">Option3</option> 
 
\t \t \t \t \t \t \t </select> 
 
\t \t \t \t \t \t </form> 
 
\t \t \t \t \t </td> 
 
\t \t \t \t </tr> 
 
\t \t \t </tbody> 
 
\t \t </table> 
 
\t </body> 
 
</html>

私はXPathをコピーするためにDevToolを使用して、私はコードを書いた:

element = Select(driver.find_element_by_xpath("//* [@id='substituteForm']/select")) 

と、私はエラーメッセージを持っている:

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: //*[@id='substituteForm']/select 

多くのXPathの組み合わせを試しましたが、それでも動作しません。

答えて

1

この目標までselect要素を待つために、コードの下に使用してみてください問題をタイミングしているように見えるが、ないXPath

DOMに表示されます:

from selenium.webdriver.common.by import By 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.support.ui import WebDriverWait as wait 

select = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//form[@id='substituteForm']/select"))) 
select.click() 
selected_option = wait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//option[@selected='selected']"))) 
assert selected_option.text == "Option3" 
+0

ヤップ。それは今働く。ありがとう:) – Surion

関連する問題