简体   繁体   English

如何使用Python的Selenium Chromedriver获得ID?

[英]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. 如果可能的话,我想直接使用Python中的Selenium Chromedriver直接获取iFrame的ID,而不使用Javascript,但是我不知道为什么会失败。

When I do this I get wrong Id result as shown below. 当我这样做时,我得到错误的ID结果,如下所示。

>>> 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: 如果我在Chrome控制台上发送以下Javascript命令,则会获得正确的框架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()使用相同的Javascript命令时,它什么也没有显示。

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

So, here I have 2 issues: 因此,这里有2个问题:

1- driver.find_element_by_class_name().id is not showing the correct Id 1- driver.find_element_by_class_name().id未显示正确的Id

2- driver.execute_script() it doesn't show anything. 2- driver.execute_script()它什么都不显示。

What I'm doing wrong? 我做错了什么?

Thanks for any help. 谢谢你的帮助。

UPDATE UPDATE

A sample html code is here https://jsfiddle.net/k3x9rsa6/1/ 示例HTML代码在这里https://jsfiddle.net/k3x9rsa6/1/

The id you're retrieving is NOT the html id attribute. 您要检索的ID不是html id属性。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM