【问题标题】:Does selenium write and read the webelement valuesselenium 是否写入和读取 webelement 值
【发布时间】:2013-01-07 19:21:35
【问题描述】:

我第一次使用 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>

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    是的,检查 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
    

    你可以选择列表、单选按钮、span 什么的,只要学习 Xpath 就值得努力。

    【讨论】:

    • click 事件是否适用于选择? `click`如何选择值
    • 是点击应该选择下拉选项我编辑了我的答案,包括如何点击科罗拉多选择。
    • 那么Read呢?
    • 不确定在 selenium 中调用 url 时的意思,然后读取它。一旦页面被拉起,页面内容就会被读取。因此,要与读取页面交互,您需要调用选择器(xpath、id、class 等...)
    • 看我说的是我们设置每个 web 元素的值的方式,同样我怎样才能获得之前设置的 web 元素中的值?
    猜你喜欢
    • 2014-04-21
    • 2015-10-18
    • 2012-06-29
    • 2016-06-28
    • 2019-12-13
    • 2017-04-28
    • 2018-05-14
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多