我对硒有问题,无法切换到框架。我尝试使用iframe的XPath,但它回答没有找到该元素。这是我正在使用的代码:
driver.switch_to_frame(driver.find_element_by_xpath('//[@id="VF"]'))
我也尝试使用CSS选手,但它给了我相同的结果。我怀疑我想选择的iframe的DIV元素正在引起此问题,但我不确定。这是Div元素的样子:
<div id="dee_0" class="ar BAResults" fframe='<iframe title="SRC" src="javascript:"<HTML></HTML>"" onload="DVFol()"></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:Userskthao.ISCDesktopAutoCompletelibsite-
packagesseleniumwebdriverremotewebdriver.py", line 368, in
find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:Userskthao.ISCDesktopAutoCompletelibsite-
packagesseleniumwebdriverremotewebdriver.py", line 858, in find_element
'value': value})['value']
File "C:Userskthao.ISCDesktopAutoCompletelibsite-
packagesseleniumwebdriverremotewebdriver.py", line 311, in execute
self.error_handler.check_response(response)
File "C:Userskthao.ISCDesktopAutoCompletelibsite-
packagesseleniumwebdriverremoteerrorhandler.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"]"}
切换到 iframe
的方法是 switch_to.frame()
,<iframe>
标签包含属性title
作为 SRC
。因此,您可以使用以下代码行:
driver.switch_to.frame(driver.find_element_by_xpath("//iframe[@title='SRC']"))