Ger Cas
Ger Cas

Reputation: 2298

How to get Id with Selenium Chromedriver with Python?

I want to get the Id of an iFrame directly with Selenium Chromedriver in Python without Javascript if possible, but I don't know why is failing.

When I do this I get wrong Id result as shown below.

>>> driver.find_element_by_class_name("MyClass").id
u'5b6a-c153-4e7f-90b2-b7e45'

If I send the following Javascript command on Chrome Console I get the correct frame Id:

> document.getElementsByClassName('MyClass')[0].id  
< "MyFrame89-0-bed65f30"

Now when I try to use the same Javascript command within driver.execute_script() it doesn't show anything.

>>> driver.execute_script("document.getElementsByClassName('MyClass')[0].id")
>>>

So, here I have 2 issues:

1- driver.find_element_by_class_name().id is not showing the correct Id

2- driver.execute_script() it doesn't show anything.

What I'm doing wrong?

Thanks for any help.

UPDATE

A sample html code is here https://jsfiddle.net/k3x9rsa6/1/

Upvotes: 0

Views: 445

Answers (1)

DMart
DMart

Reputation: 2461

The id you're retrieving is NOT the html id attribute. Try retrieving it one of these ways:

driver.find_element_by_class_name("MyClass").get_attribute("id")

Or

driver.find_element_by_class_name("MyClass").get_property("id")

Upvotes: 1

Related Questions