如何通过Selenium和Python根据html查找元素的Xpath [英] How to find the Xpath of the element as per the html through Selenium and Python

查看:129
本文介绍了如何通过Selenium和Python根据html查找元素的Xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中尝试过类似的操作,但我不想使用Selenium单击十字架.

Tried something like this in python unable I want to click on cross using Selenium.

driver.find_element_by_xpath("//span[contains(@onclick, 'parent.$WZRK_WR.closeIframe('60005','intentPreview');')]").click()

推荐答案

根据您共享的 HTML ,单击表示为 X 的元素,首先需要在切换到<iframe>时诱使 WebDriverWait 并再次诱使 WebDriverWait 使所需的元素可点击,您可以使用以下解决方案:

As per the HTML you have shared to click on the element depicted as X, first you need to induce WebDriverWait while switching to the <iframe> and again induce WebDriverWait for the desired element to be clickable and you can use the following solution:

WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='wiz-iframe-intent']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='CT_Interstitial']//span[@class='CT_InterstitialClose']"))).click()

注意:您必须添加以下导入:

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

这篇关于如何通过Selenium和Python根据html查找元素的Xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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