无法通过 iFrame 内的 xpath 单击元素 - Selenium

我知道当我想单击其中的元素时必须切换到 iframe。iFrame 的开头如下所示:

<iframe src="about:blank" name="tool_content" id="tool_content" class="tool_launch" allowfullscreen="allowfullscreen" webkitallowfullscreen="true" mozallowfullscreen="true" tabindex="0" title="Tool Content" style="height: 100%; width: 100%;" allow="geolocation *; microphone *; camera *; midi *; encrypted-media *; autoplay *" data-lti-launch="true"></iframe>

我切换到它使用driver.switch_to.frame(driver.find_element_by_class_name("tool_launch"))

然后,我尝试使用确切的 xpath 单击 Iframe 内的“加入”按钮:

driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div/div/div[3]/div[1]/div[2]/div/div/div/div/div/div/div/div/div/div/table/tbody/tr/td[4]/div/div[1]/a").click()

但它一直说它不存在。为什么?该框架内没有其他 iframe,并且此 xpath完全 100%在 iframe 内。它可以识别其他元素,为什么这个不能呢?

这是加入按钮元素行:

<a target="_blank" href="/lti/rich/j/96494344321?oauth_consumer_key=AAPUZMZcQCSN85nnG74vOQ&amp;lti_scid=ee49a632bee823b3046456c29c5eb4064ee1e3790f433af82bebaf2752bde531" class="ant-btn ant-table-span" style=""> Join</a>

iframe 如下所示 - 我想单击加入按钮:

https://img1.sycdn.imooc.com/652657c5000102f314930363.jpg

它在一张桌子里面。

我可以选择“以前的会议”选项卡等内容,但无法使用“加入”或“邀请”按钮。

这是完整的 HTML

我无法粘贴它,因为这只能在检查元素中查看(查看源代码时,您看不到所有这些),并且我一次只能复制一个元素。

我在顶部突出显示了 iframe 的起始位置以及我要单击的“加入”按钮的位置。

这两个图像只是一个接一个地延续。

https://img1.sycdn.imooc.com/652657d700018c4816620733.jpg

https://img1.sycdn.imooc.com/652657e00001b2cf16320705.jpg

犯罪嫌疑人X
浏览 141回答 1
1回答

子衿沉夜

尝试等待并单击该元素。尽量不要使用xpath。elem=WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.ant-btn.ant-table-span")))elem.click()进口from selenium.webdriver.common.by import Byfrom selenium.webdriver.support.ui import WebDriverWait&nbsp;from selenium.webdriver.support import expected_conditions as EC
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python