简体   繁体   English

Python-Selenium-在iframe中导航时遇到的问题

[英]Python - Selenium - Problems navigating in iframes

I've been trying to log into a website using selenium. 我一直在尝试使用硒登录网站。 I have the following problem > i manage to open an iframe (it is correctly displayed after i click on the login button) but i've been unable to move into it. 我遇到以下问题>我设法打开一个iframe(单击“登录”按钮后,它会正确显示),但我一直无法进入。 I've been trying to find it via it's Id and name but i somehow can't find it... 我一直在尝试通过它的ID和名称来查找它,但是我却以某种方式找不到它...

The line looks like: 这行看起来像:

driver.switch_to.frame(driver.find_element_by_id("frameid"))

I get an error "no such element". 我收到一个错误“没有这样的元素”。 I checked 1000 times the id and it is correct! 我检查了1000次ID,这是正确的! The frame i want to switch to is not inside another frame. 我要切换到的框架不在另一个框架内。

Any suggestions ? 有什么建议么 ?

It seem's that the id is somehow dynamically allocated but always starts with the same characters. id似乎是以某种方式动态分配的,但始终以相同的字符开头。 I can't use a regex-based search apparently 我显然不能使用基于正则表达式的搜索

In this case you can locate it with an XPath and starts-with() function: 在这种情况下,您可以使用XPathstarts-with()函数找到它:

frame = driver.find_element_by_xpath("//iframe[starts-with(@id, 'something')]")
driver.switch_to.frame(frame)

Or, with a CSS "begin with" selector : 或者,使用CSS“开始于”选择器

frame = driver.find_element_by_css_selector("iframe[id^=something]")
driver.switch_to.frame(frame)

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

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