python硒找不到iframe xpath [英] python selenium cant find iframe xpath

查看:78
本文介绍了python硒找不到iframe xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到Selenium问题,无法切换到框架.我尝试使用iframe的xpath,但它答复未找到该元素.这是我正在使用的代码:

I am having an issue with Selenium and not being able to switch to a frame. I have tried using the xpath of iframe but it replies that the element was not found. This was the code I was using:

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

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

I have also tried using a css-selector but it gives me the same result. I suspect that the div element that holds the iframe that I want to select is causing this issue but I'm not sure. Here's what that div element looks like:

<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

edit this is the iframe I'm trying to target

<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"]"}

推荐答案

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

The method to switch to an iframe is switch_to.frame() and the <iframe> tag contains the attribute title as SRC. So you can use the following line of code :

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

这篇关于python硒找不到iframe xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆