简体   繁体   中英

Selenium get value python

I am new in using selenium, I would like to get this value of the line highlighted. While I am using the get attribute function, it pop up an error."TypeError: get_attribute() missing 1 required positional argument: 'name'". How can I solve it or any part is missing?

def HSI_realtimeprice():

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.hkex.com.hk/Products/Listed-Derivatives/Equity-Index/Hang-Seng-Index-(HSI)/Hang-Seng-Index-Options?sc_lang=zh-HK#&product=HSI')
hsi_price = driver.find_element_by_xpath('//div[@class="ls"]').get_attribute()
print(hsi_price)

HSI_realtimeprice()

在此处输入图像描述

Try using hsi_price.text if you mean to extract "12,639.39"

get_attribute() should be used when you wanna extract attributes like href , src , etc.

For this element

<img src="http://example.com/my-image.img"/>

the code

element.get_attribute('src')

gets you http://example.com/my-image.img

You should input the attributes of what you want to achieve.

hsi_price = driver.find_element_by_xpath('//div[@class="ls"]').get_attribute('insert_here')

And it looks like you want to get the text, use innerHTML :

driver.find_element_by_xpath('//div[@class="ls"]').get_attribute('innerHTML')

Although that can also be achieved with .text :

hsi_price = driver.find_element_by_xpath('//div[@class="ls"]').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