概述
<html>
<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>
<input name="xmpy" maxlength="80" tabindex="2" value="" οnkeyup="limitLength(this.value,80,'姓名拼音',this.id)" style="ime-mode:disabled" class="text" id="xmpy" type="text">
<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")
for i in range(1, 4):
print str(i)
if i == 3:
print "**************"
browser.find_element_by_id("xmpy").send_keys("666-666")
text = browser.find_element_by_id("xmpy").get_attribute("value")
print len(text)
print text
browser.quit()
最后
以上就是美满树叶为你收集整理的python获取页面元素的内容及长度的全部内容,希望文章能够帮你解决python获取页面元素的内容及长度所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复