本文介绍了如何使用python硒单击iframe内的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
他是我的样本片段.我想使用python硒单击按钮1034-btnIconEl.
He is my sample snippet. i want to click the button-1034-btnIconEl using python selenium.
<html>
<body>
<div class="x-container x-border-item x-box-item x-container-default x-layout-fit" id="iframes" >
<iframe ></iframe>
<iframe class="x-component x-fit-item x-component-default" frameborder="0" id="rpIFrame-1239">
<html>
<body>
<div> .....many divs
<div>
<a><span><span id="button-1034-btnIconEl"></span></span></a>
</div>
</div>
</body>
</html>
</iframe>
我尝试过
driver.switch_to.frame(1)
driver.find_element(By.XPATH, "//span[contains(@id,'button-1034-btnIconEl')]").click()
但得到
请帮助我进行遍历.
推荐答案
以 id 为 button-1034-btnIconEl 的按钮上的click()
为所需元素位于<iframe>
中,因此您必须:
To click()
on the button with id as button-1034-btnIconEl as the the desired element is within an <iframe>
so you have to:
- 诱导 WebDriverWait 以使所需的框架可用并切换到.
- 诱导 WebDriverWait 以使所需的元素可点击.
-
您可以使用以下定位器策略:
- Induce WebDriverWait for the desired frame to be available and switch to it.
- Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
-
使用
CSS_SELECTOR
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe.x-component.x-fit-item.x-component-default[id^='rpIFrame-']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a > span > span[id^='button'][id$='btnIconEl']"))).click()
使用XPATH
:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@class='x-component x-fit-item x-component-default' and starts-with(@id, 'rpIFrame-')]")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//a/span/span[starts-with(@id, 'button') and contains(@id, 'btnIconEl')]"))).click()
这篇关于如何使用python硒单击iframe内的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!