0

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/

4
  • What is the correct id? can you share the relevant HTML? Commented Aug 28, 2019 at 19:39
  • @DebanjanB Hello, thanks for answer. A sample code is here jsfiddle.net/k3x9rsa6/1 Commented Aug 28, 2019 at 20:31
  • 1
    id is an HTML attribute and not a class field. Try .get_attribute("id"). Commented Aug 28, 2019 at 22:37
  • @SiKing Excellent. Thanks so much for your help. Now it works. You can put it as answer. Commented Aug 28, 2019 at 23:01

1 Answer 1

1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.