<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>获取option的值和文本</title>
</head>
<body>
<form>
<select id="ss">
<option value="1111">麦当劳</option>
<option value="2222">肯德基</option>
<option value="3333">必胜客</option>
<option value="4444">汉堡王</option>
</select>
</form>
<div style="background-color: rgb(255, 238, 221);" id="status" class="errors">您输入的用户名或密码有误。</div>
<a href="javascript:alert('麦当劳')">javascript</a>
<script type="text/javascript">
var oSelect=document.getElementByIdx_x_x("ss");
oSelect.οnchange=function(){ //当选项改变时触发
var valOption=this.options[this.selectedIndex].value;//获取option的value
alert(valOption);
var txtOption=this.options[this.selectedIndex].innerHTML;//获取option中间的文本
alert(txtOption);
}
</script>
</body>
</html>
-----------------------------------------------------------------------------------------------------------------------
# coding:utf-8
from selenium import webdriver
from PIL import ImageGrab
import time
browser = webdriver.Firefox()
url = 'file:///C:/Users/liqq/Desktop/option.html'
browser.get(url)
select = browser.find_element_by_link_text("javascript")
select.click()
a = browser.switch_to_alert()
print a.text
if a.text.encode('utf-8') == "麦当劳":
print "--------------------"
time.sleep(2)
filename = "D:\\work\\YZ\\trunk\\result\\screenshot\\" + 'test_alert.jpg'
try:
browser.find_element_by_id('NoSuch_element')
except:
browser.get_screenshot_as_file(filename)
select.click()
try:
browser.find_element_by_id('NoSuch_element')
except:
im = ImageGrab.grab()
im.save("D:\\1.jpg")
browser.quit()