python selenium 点击按钮动态刷新iframe_如何使用python硒单击iframe内的按钮

在使用Python Selenium尝试点击ID为'button-1034-btnIconEl'的按钮时遇到问题,报错'NoSuchElementException'。解决方案包括使用WebDriverWait等待iframe和元素变为可用,然后切换到iframe并点击元素。可以使用CSS_SELECTOR或XPATH定位策略实现。
摘要由CSDN通过智能技术生成

He is my sample snippet. i want to click the button-1034-btnIconEl using python selenium.

.....many divs

i tried this

driver.switch_to.frame(1)

driver.find_element(By.XPATH, "//span[contains(@id,'button-1034-btnIconEl')]").click()

but getting

"NoSuchElementException: no such element:"

Please help me to traverse this.

解决方案

To click() on the button with id as button-1034-btnIconEl as the the desired element is within an so you have to:

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:

Using 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()

Using 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()

Here you can find a relevant discussion on Ways to deal with #document under iframe

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值