python selenium找不到iframe xpath

2024-05-18 13:57:28 发布

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

我有一个硒的问题,不能切换到一个框架。我没有尝试使用xpath元素的iframe。这是我使用的代码:

driver.switch_to_frame(driver.find_element_by_xpath('//[@id="VF"]'))

我也尝试过使用css选择器,但它给了我相同的结果。我怀疑保存我要选择的iframe的div元素导致了这个问题,但我不确定。下面是div元素的外观:

^{pr2}$

任何帮助都将不胜感激。在

编辑 这就是我要瞄准的iframe

<iframe name="VF" title="SRC" id="VF" src='javascript:"<HTML></HTML>"' frameborder="0" scrolling="auto" style="left: 0px; top: 0px; width: 830px; height: 490px; background-color: transparent;" allowtransparency="true" onload="F(0,302899400).ol()">

堆栈跟踪

Traceback (most recent call last):
File "C:/Users/kthao.ISC/PycharmProjects/AutoComplete/test2.py", line 32, in 
<module>
chrome.switch_to_frame(chrome.find_element_by_xpath('//*
[@id="VF"]'))
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 368, in 
find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 858, in find_element
'value': value})['value']
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\webdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:\Users\kthao.ISC\Desktop\AutoComplete\lib\site-
packages\selenium\webdriver\remote\errorhandler.py", line 237, in 
check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: 
Unable to locate element: {"method":"xpath","selector":"//*
[@id="VF"]"}

Tags: inpyseleniumlineelementfindautocompleteusers
1条回答
网友
1楼 · 发布于 2024-05-18 13:57:28

切换到iframe的方法是switch_to.frame(),并且<iframe>标记包含属性title,即SRC。因此您可以使用以下代码行:

 driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@title='SRC']"))

相关问题 更多 >

    热门问题