检索带有Selenium WebDriver和Python的动态框架名称? [英] retrieve dynamic frame names w/ Selenium WebDriver and Python?
问题描述
我需要在一个特定的数据库上运行数千个搜索,并且我正在使用Python和Selenium WebDriver对其进行自动化.事实是,包含搜索结果的框架的名称是动态生成的.该名称始终以相同的方式开头(例如,"results_frame"),然后跟着一堆每次更改的数字(例如"results_frame1298120910290").因此,基于几个小时的搜索,我尝试使用xpath:
I need to run thousands of searches on a certain database and I'm using Python and Selenium WebDriver to automate it. The thing is, the frame that contains the search results has a name that is dynamically generated. That name always begins the same way (say, "results_frame") and then it's followed by a bunch of numbers that change every time (like "results_frame1298120910290"). So, based on a few hours of googling, I tried using xpath:
framename = driver.find_elements_by_xpath("//frame[contains(@name, 'results_frame')]")
driver.switch_to_frame(framename)
不好.当我打印'framename'时,我得到的是这样:
No good. When I print 'framename', what I get is this:
[<selenium.webdriver.remote.webelement.WebElement object at 0x101206350>, <selenium.webdriver.remote.webelement.WebElement object at 0x101206390>]
代替框架的实际名称.
我还尝试使用索引(如在driver.switch_to_frame(0)
,driver.switch_to_frame(1)
等中),如建议的
I've also tried using indexes (as in driver.switch_to_frame(0)
, driver.switch_to_frame(1)
, etc), as suggested here, but no good either.
那么,当动态生成框架名称时,如何检索该框架的名称?有什么想法吗?
So, how can I retrieve a frame's name when that name is dynamically generated? Any thoughts?
(Python 2.7.5,Selenium 2.2.0,Mac OS X 10.6.8,Firefox 22.0)
(Python 2.7.5, Selenium 2.2.0, Mac OS X 10.6.8, Firefox 22.0)
推荐答案
find_elements_by_xpath
返回类型为WebElement
的对象.您需要使用get_attribute
从WebElement
检索属性的值.
find_elements_by_xpath
returns an object of type WebElement
. You need to use get_attribute
to retrieve the value of an attribute from the WebElement
.
framename = driver.find_element_by_xpath("//frame[contains(@name, 'results_frame')]").get_attribute("name")
这应该返回您要查找的名称.
That should return the name you're looking for.
这篇关于检索带有Selenium WebDriver和Python的动态框架名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!