如何通过 JS 脚本创建的 Selenium 获取元素

How to take elements by Selenium which created by JS script

我正在尝试使用 Selenium(python 绑定)进行自动化测试,特别是想登录 tokmonet.zendesk.com。
我创建了一个脚本,它通过 id 获取电子邮件字段、密码字段和登录按钮。
但是当我 运行 脚本时它失败了

selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"user_email"}  

使用 Firebug 检查页面我看到了这些元素。但是当试图用 Firefinder 获取它们时,它做不到。 所以,我执行

html_source = driver.page_source  
print(html_source)

并获得唯一的

<html xmlns="http://www.w3.org/1999/xhtml"><head></head><body></body></html>

当我检查页面源代码时,它只包含 js 脚本,没有任何标记。

请指教我如何处理这些元素?

我看到您尝试登录的元素位于 tokmonet.zendesk.com 的 iframe 中,因此您无法获取这些元素。要处理这种情况,请先尝试切换到 iframe,然后再获取元素。这是 Java -

中的操作方法
driver.switchTo().frame(driver.findElement(By.tagName("iframe")));
(new WebDriverWait(driver, 20))
          .until(ExpectedConditions.presenceOfElementLocated(By.id("user_email"))).sendKeys("username");
//Similarly you can get other elements too

您同样可以用其他语言实现它。希望这有帮助。

您需要切换到IFRAME,然后send_keys()切换到您可以通过ID找到的元素。如果您需要访问 IFRAME.

之外的元素,请不要忘记切换回默认内容
driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
driver.find_element_by_id("user_email").send_keys("username")
driver.find_element_by_id("user_password").send_keys("password")
// do whatever else
driver.switch_to.default_content()