简体   繁体   English

在Selenium Webdriver ruby​​脚本中获取元素值

[英]Get element value in Selenium webdriver ruby script

I am trying to put some asserts by getting value and then comparing them. 我试图通过获取价值然后进行比较来提出一些主张。 But, the get value statement always returns nil. 但是,get value语句始终返回nil。

Desired value:- 期望值:-

<span class="contractSalesPrice.contractSalesPrice">$314,507.30</span>

I tried following ways: - 我尝试了以下方法:-

element1 = browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span")

browser.execute_script("return arguments[0]", element1)

browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").attribute('value')

browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").value

What could be wrong? 有什么事吗

Pls help, Thank You in advance. 请帮助,谢谢您。

Abhishek 阿布舍克

You need to use #text method. 您需要使用#text方法。

browser.find_element(:css,"#total > tbody > tr > td:nth-child(4) > span").text

attribute method is used to get the value of attributes of HTML elements. attribute方法用于获取HTML元素的属性值。 In your case, there is no attribute called 'value' in span element, thus the method returns nil . 在您的情况下, span元素中没有名为'value'属性,因此该方法返回nil For more information read the documentation I linked. 有关更多信息,请阅读我链接的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM