python获取页面元素的内容及长度

来源:互联网 发布:知乎手机最好的变声器 编辑:程序博客网 时间:2024/06/04 22:54

 <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="" onkeyup="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.onchange=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()


0 0
原创粉丝点击