Python Selenium 问题 导航 在嵌入框架中

2024-04-19 19:29:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在尝试使用selenium登录一个网站。我有以下问题>;我设法打开一个iframe(单击“登录”按钮后它会正确显示),但我无法进入它。我一直试图通过它的身份证和名字找到它,但不知为什么我找不到它。。。你知道吗

这条线看起来像:

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

我得到一个错误“没有这样的元素”。我检查了1000次身份证,它是正确的!我要切换到的帧不在另一帧中。你知道吗

有什么建议吗?你知道吗


Tags: togtby网站driverseleniumelementfind
1条回答
网友
1楼 · 发布于 2024-04-19 19:29:52

It seem's that the id is somehow dynamically allocated but always starts with the same characters. I can't use a regex-based search apparently

在这种情况下,您可以使用XPath^{}函数来定位它:

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

或者,使用CSS "begin with" selector

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

相关问题 更多 >