使用python selenium从HTML标签中提取占位符

我正在使用以下 python 代码来启动 Firefox 网页。


from selenium import webdriver

from selenium.webdriver.common.keys import Keys

driver= webdriver.Firefox()

driver.get("https://www.quora.com")

启动后,如果我知道这个标签的 xpath。


<input  

class="text header_login_text_box ignore_interaction" 

type="text" 

name="email" tabindex="1"

data-group="js-editable"

placeholder="Email"

w2cid="wZgD2YHa18" 

id="__w2_wZgD2YHa18_email">

如果我现在是属性的名称,我可以使用以下命令在 python 上使用 selenium webdriver 提取属性。


dict['attribute'] = driver.find_element_by_xpath(x_path).get_attribute(attribute)

所以我的输出将是


dict = { 'attribute':value}

请帮助我找出提取所有属性及其值的方法,即使我不知道它具有的所有属性是什么。我的预期输出是


dict = { "class" : "text header_login_text_box ignore_interaction" 

        "type" : "text" 

        "name":"email" 

         "tabindex" : "1"

        "data-group" : "js-editable"

        "placeholder" : "Email"

        "w2cid" : "wZgD2YHa18" 

        "id" : "__w2_wZgD2YHa18_email"

        }

我不确定这有多大可能,但我希望像在字典中一样,即使不知道密钥,我们也可以提取数据。谢谢


UYOU
浏览 323回答 3
3回答

Smart猫小萌

获取placeholder属性使用get_attribute()element.get_attribute('placeholder')

qq_遁去的一_1

定义要提取占位符的输入标签的 xpath。xpath_input = "//input[@id='__w2_wZgD2YHa18_email']"driver.find_element_by_xpath(xpath_input)获取元素后,您可以通过以下方式提取placeholder(“电子邮件”)get_attribute("placeholder")
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python