簡體   English   中英

Python Selenium選擇一個下拉選項,然后單擊“提交”

[英]Python Selenium select a drop down option and hit submit

我已經專門嘗試過一些示例,這些示例與該問題有關,但似乎沒有用? 如果我缺少某些東西,有人可以指出我正確的方向嗎? 我基本上只是想從下拉列表中選擇“低”選項,然后點擊“提交”。

<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>")

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"))

有關更多詳細信息,請轉到此處

我讓它工作了。...不是最干凈的解決方案,但我發現它可以工作.....

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM