如何在python硒中获取属性?

问题描述 投票:0回答:1
driver.get("https://es.quora.com/translate_answer?source=overflow_menu&translate_answer=184353372")

copy = driver.find_elements_by_xpath("//*[(self::img or self::p)and contains(@class, 'ui_qtext')]"and"//*[(self::img or self::p)and contains(@class, 'ui_qtext')]")
paste = driver.find_element_by_xpath("//div[@placeholder='Traducir la respuesta']/div")
paste.click()

for text in copy:
    copy = text.get_attribute("src") #and ("textContent")
    actions = ActionChains(driver)
    actions.send_keys(copy)
    actions.perform()

我想在同一变量中同时获取textContent和src的属性。我希望它们“组合”的原因是因为我想按顺序复制文本和src(图像的网址)。

python selenium selenium-chromedriver copy-paste src
1个回答
0
投票

'Get_attribute'模块用于获取元素中的数据,要接收的数据是文本数据

示例:

<img width="300" height="150" src="https://cdn.shortpixel.ai/client/to_webp,q_glossy,ret_img,w_300/https://www.newsinlevels.com/wp-content/uploads/2020/05/773x435_cmsv2_f62ba99a-6c0e-5be4-aee3-b0ddc560f280-4645326-300x150.jpg"

如果您对此元素进行'get_attribute(“ src”)',则会获得链接输出

但您想获取“文本”数据,则应使用此:

for text in copy:
    actions = ActionChains(driver)
    actions.send_keys(text.text)
    actions.perform()
© www.soinside.com 2019 - 2024. All rights reserved.