python selenium无法找到iframe xpath

时间:2017-12-20 03:25:11

标签: python selenium iframe xpath

我遇到了Selenium的问题而无法切换到框架。我已经尝试使用iframe的xpath,但它回复说找不到该元素。这是我使用的代码:

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

我也尝试过使用css-selector,但它给了我相同的结果。我怀疑包含我想要选择的iframe的div元素导致了这个问题,但我不确定。这是div元素的样子:

<div id="dee_0" class="ar BAResults" fframe='<iframe title="SRC" src="javascript:&quot;&lt;HTML&gt;&lt;/HTML&gt;&quot;" onload="DVFol&#40;&#41;"></iframe>

非常感谢任何帮助。

编辑 这是我尝试定位的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"]"}

1 个答案:

答案 0 :(得分:1)

切换为iframe的方法为 switch_to.frame() <iframe>标记包含属性title {{1 }} 即可。因此,您可以使用以下代码行:

SRC