2014-05-13 409 views
1

我試過了具體的例子,我發現這個問題與這個問題有關,他們似乎沒有工作?如果我錯過了這裏的HTML,有人可以指引我正確的方向嗎?我基本上只是試圖從下拉列表中選擇'低'選項並點擊提交。Python Selenium選擇一個下拉選項並點擊提交

<form action="#" method="POST"> 
     <p>Security Level is currently <em>high</em>.<p> 
     <p>You can set the security level to low, medium or high.</p> 
     <p>The security level changes the vulnerability level of DVWA.</p> 

     <select name="security"> 
      <option value="low">low</option><option value="medium">medium</option><option value="high" selected="selected">high</option> 
     </select> 
     <input type="submit" value="Submit" name="seclev_submit"> 
    </form> 

下面是我的代碼:

find_element_by_xpath("//select[@name='security']/option[@value='low']").click() 

我已經試過這些方式試圖讓提交按鈕的工作:

driver.select_by_value("//select[@id='security']").click() 
driver.find_find_element_by_name("submit").click() 

任何指針?

更新:

這似乎工作,但現在我不能確認爲什麼?我添加了附加信息去新頁面,然後停止工作?

driver.get('http://dvwa/security.php') 
el = driver.find_element_by_name('security') 
for option in el.find_elements_by_tag_name('option'):  
    if option.text == 'low':   
     option.click() 
driver.find_element_by_name('seclev_submit').click() 
select = Select(driver.find_element_by_xpath("//select[@name='security']")) 
select.select_by_value("low") 


#Check for XSS 
driver.get('http://dvwa/vulnerabilities/xss_r/') 
elem = driver.find_element_by_name("name") 
elem.send_keys("<script>alert(document.cookie);</script>") 
+0

似乎這是你在找什麼:http://sqa.stackexchange.com/questions/1355/what-is-the-correct-way-to-select-an-option-using-seleniums-python-webdriver – quant

+0

如果你嘗試使用? ()選擇[@ name ='security']/option [text()='low']「)。click() – Cas

回答

0

Select是一個用於下拉菜單的類。請嘗試以下操作:

select = Select(driver.find_element_by_xpath("//select[@name='security']")) 
select.select_by_value("medium") 

,或者您也可以按名稱創建select對象元素,如:

select = Select(driver.find_element_by_name("security")) 

有關詳細信息,請訪問here

0

我得到這個工作... 。不是最乾淨的解決方案,但我發現它的工作.....

driver.get('http://dvwa/security.php') 
driver.implicitly_wait(15) 
el = driver.find_element_by_name('security') 
for option in el.find_elements_by_tag_name('option'):  
    if option.text == 'low':   
    option.click() 
driver.implicitly_wait(15) 
select = Select(driver.find_element_by_xpath("//select[@name='security']")) 
select.select_by_value("low") 
driver.implicitly_wait(15) 
driver.find_element_by_name('seclev_submit').click() 
driver.implicitly_wait(15) 
相關問題