5

我第一次使用 Python2.7 和 selenium 进行自动化。现在我可以写和读下面的 HTML 内容了吗?

单选按钮

<form name="myWebForm" action="mailto:youremail@email.com" method="post">
<h4>Please select your favorite food category.</h4>
<input type="radio" name="food" /> : Italian<br />
<input type="radio" name="food" /> : Greek<br />
<input type="radio" name="food" /> : Chinese<br />
<h4>Please select your gender.</h4>
<input type="radio" name="gender" /> : Male<br />
<input type="radio" name="gender" /> : Female<br />
</form>

单选列表

  <select size="3" name="selectionField" multiple="yes" > 
      <option value="CA" >California -- CA </option>
      <option value="CO" >Colorado -- CO</option>
      <option value="CN" >Connecticut -- CN</option>
    </select>

定义列表

<dl>
<dt>Coffee</dt>
<dd>- black hot drink</dd>
<dt>Milk</dt>
<dd>- white cold drink</dd>
</dl> 

复选框

<form name="myWebForm" action="mailto:youremail@email.com" method="post">
<p>Please select every sport that you play.</p>

Soccer: <input type="checkbox" name="sports" value="soccer"  /><br />

Football: <input type="checkbox" name="sports" value="football"  /><br /> 

Baseball: <input type="checkbox" name="sports" value="baseball"  /><br /> 

Basketball: <input type="checkbox" name="sports" value="basketball"  />

</form>
4

1 回答 1

7

是的,检查 xpath

x = brower.select_element_by_xpath('//option[contains(text(), "CO"]')
x.text (print the div text) 
x.click() clicks the div

要不就

brower.select_element_by_xpath('//option[contains(text(), "CO"]').click()

阅读列表,这应该有效;

for i in browers.select_elements_by_xpath('//select[@name="selectionField"]//option'):
    print i.text

您可以选择列表、单选按钮、跨度等,只要学习 Xpath 就值得付出努力。

于 2013-01-07T20:07:39.027 回答