多个iframe中的Python硒定位元素 [英] Python selenium locate element in multiple iframes
问题描述
我刚接触Selenium for Python,并试图在多个iframe中定位元素. 这是我看到的DOM元素.
I am new to Selenium for Python and was trying to locate element in multiple iframes. This is the DOM element I can see.
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>...</head>
<body>
<form>
...
<div class="page">
<div class="main clear" style="z-index: 20; position:relative;">
<div id="placeOrder">
<iframe src="BuyFlow.aspx" frameborder="0" width="1150" height="950">
#document
<html>
<body>
<form>
...
<iframe id="CreativeLiftFrame">
#document
<html>
...
<body id="multiple-addresses">
...
</body>
</html>
</iframe>
</form>
</body>
</html>
</iframe>
</div>
</div>
</div>
</form>
</body>
</html>
我要做的是获取第二个<iframe>
的<body>
标记的id
名称.
What I want to do is to get the <body>
tag's id
name of second <iframe>
.
那是"multiple-addresses"
.
为此,我编写了如下代码.
In order to do that I have written my code as follows.
# Switch to the first iframe
iframe = driver.find_element(By.TAG_NAME, 'iframe')
driver.switch_to_frame(iframe)
# Fill in Address and ZipCode inputbox and submit form
address_input.send_keys(address)
postcode_input.send_keys(postcode)
postcode_input.send_keys(Keys.RETURN)
# Check Available - Inner iframe
second_iframe = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.TAG_NAME, 'iframe')))
driver.switch_to_frame(second_iframe)
print(second_iframe.get_attribute("id")
body = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.TAG_NAME, 'body')))
print(body.get_attribute("id")
结果,我在控制台上获得了2个输出.
As a result, I get 2 outputs on console.
CreativeLiftFrame
None
如您所见,Selenium驱动程序位于第二个iframe
,但是在第二个iframe
中找不到body
标签的id
.
As you can see, selenium driver located the second iframe
, but can't locate body
tag's id
in SECOND iframe
.
我不确定该如何处理.
推荐答案
根据您共享的 HTML ,以检索主体的 id 第二个孩子<frame>
的em>标签,您可以使用以下代码:
As per the HTML you have shared to retrieve the id of the body tag of the second child <frame>
you can use the following code :
# Switch to the first iframe
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@src='BuyFlow.aspx']")))
# Fill in Address and ZipCode inputbox and submit form
address_input.send_keys(address)
postcode_input.send_keys(postcode)
postcode_input.send_keys(Keys.RETURN)
# Check Available - Inner iframe
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"CreativeLiftFrame")))
print(WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, 'body'))).get_attribute("id"))
这篇关于多个iframe中的Python硒定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!