我是使用selenium的新手,我想突出显示这一行的值。当我使用get attribute函数时,它弹出一个错误:“TypeError: get_attribute()缺少一个必需的位置参数:'name'”。我怎么才能解决这个问题,或者缺少任何部分?
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()
发布于 2020-07-22 03:43:16
你应该输入你想要实现的属性。
hsi_price = driver.find_element_by_xpath('//div[@class="ls"]').get_attribute('insert_here')
看起来您想要获取文本,使用innerHTML
driver.find_element_by_xpath('//div[@class="ls"]').get_attribute('innerHTML')
尽管这也可以通过.text
来实现
hsi_price = driver.find_element_by_xpath('//div[@class="ls"]').text
发布于 2020-07-22 03:31:36
如果您想提取"12,639.39“,请尝试使用hsi_price.text
当你想提取href
、src
等属性时,应该使用get_attribute()
。
对于此元素
<img src="http://example.com/my-image.img"/>
代码
element.get_attribute('src')
获取http://example.com/my-image.img
https://stackoverflow.com/questions/63026058
复制相似问题