Lucas Duarte
Lucas Duarte

Reputation: 1

get_attribute method doesn't work on Python with Selenium

So, I'm having trouble getting a element from a web page with Selenium and Python

The div in question is:

<div class="col-sm-6 col-lg-4 broker-details">
    <div class=" ">
        <h6>MICHAEL CORLEONE</h6>
        <div> <strong>CODE: </strong> <span> 123456-X</span> </div>
        <div> <strong>Registration Date: </strong><span>18/03/2021</span> </div>
        <div> <strong>Situation: </strong><span>Active</span> </div>
        <form action="/citizen/details" method="post">
            <input type="hidden" name="registerNumber" value="123456-X">
            <button class="btn btn-primary" type="submit"> See More </button>
        </form>
    </div>
</div>

when I use find_elements() to return the class elements as:

#setting the driver
driver = webdriver.Chrome(service=service, options=options)

driver.find_elements(By.CLASS_NAME, 'broker-details')[0].text

it returns something like:

'MICHAEL CORLEONE\nCODE: 123456-X\nRegistration Date: 18/03/2021\nSituation: Active\nSee More'

but when I try to use get_attribute to get a specific element, like:

driver.find_elements(By.CLASS_NAME, 'broker-details')[0].get_attribute('span')

it returns nothing, should I be referencing the blank space class? didn't even know it was possible, is there something im missing? I don't really know much about html/css/python please help.

Upvotes: 0

Views: 42

Answers (1)

JeffC
JeffC

Reputation: 25531

.get_attribute() is working fine, you are using it incorrectly. You are trying to use it to retrieve child elements but that's not it's purpose.

<button class="btn btn-primary" type="submit">

Given the HTML above, .get_attribute("class") would return "btn btn-primary" or .get_attribute("type") would return "submit". It's used to return an attribute of an element, attribute being class or type. See the docs for more info.

Upvotes: 0

Related Questions