0

I am using the following python code to launch the Firefox webpage.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver= webdriver.Firefox()
driver.get("https://www.quora.com")

After launching if somehow I know the xpath of this tag.

<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">

I can extract attribute using selenium webdriver on python using the following command if I now the name of the attribute.

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

so my output will be

dict = { 'attribute':value}

Please help me to figure out the way to extract all the attributes with its value even I don't known what are all the attributes it has. My expected output would be

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"
        }

I am not sure How far it is possible, but I am expecting like in dictionaries we can extract data even without knowing the keys. Thank you

TrebledJ
  • 8,713
  • 7
  • 26
  • 48

1 Answers1

0

use .attrs

import bs4

html = '''<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">'''

soup = bs4.BeautifulSoup(html, 'html.parser')


for tag in soup:
    attr_dict = (tag.attrs)

output: print (attr_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'}
chitown88
  • 27,527
  • 4
  • 30
  • 59