简体   繁体   中英

Get value in selenium python

I have an element:

<span class="a-color-price hlb-price a-inline-block a-text-bold">$399.98</span>

and I want to extract the price value in dollars which appears in the element's text.

I have tried:

driver.get_element_by_xpath('//*[@id="nav-search"]/form/div[2]/div/input').get_attribute("value")

What about:

SpanVariable = driver.get_element_by_xpath('Put Xpath Here')

SpanVariableValue = SpanVariable.text
print SpanVariableValue  # $399.98

You need to grab the text of the element you are looking at. Place the element you located in a variable, and then you can call selenium functions to it. In this case, .text grabs the text of your span element for you.

I'm not sure what you're doing wrong, it should be pretty simple. I use C# but based on what you've given I'd try this. And if this doesn't work, use a upper case 't' for the ending '.Text;' part of the call.

my_spanText = driver.get_element_by_xpath("//span[@class='a-color-price hlb-price a-inline-block a-text-bold']").text;

This one worked for me:

element=driver.find_element_by_xpath('//*[@id="hlb-subcart"]/div[1]/span/span[2]').text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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