简体   繁体   English

在细分的网页上使用 selenium driver.find_element().click()

[英]Using selenium driver.find_element().click() on a subdivided webpage

First of all, I'm completely new in web scraping, html and selenium, so my question might not seem meaningful to you.首先,我对网页抓取、html 和 selenium 完全陌生,所以我的问题对您来说可能没有意义。

What I'm trying to do: automated click on an image on a webpage.我想要做的是:自动点击网页上的图片。 Manual click on this image allows to display new information on a part of the webpage.手动单击此图像可以在网页的一部分上显示新信息。

How I tried to do it: I found something specific to this image, for example:我是如何尝试的:我发现了一些特定于该图像的内容,例如:

<img src="images/btn_qmj.gif" border="0">

So I just entered in python:所以我刚刚输入python:

xpath = '//img[@src="images/btn_qmj.gif"]'
driver.find_elements_by_xpath(xpath)

Problem: this returns me an empty list.问题:这会返回一个空列表。 I partially understood why, but I don't have any solution.我部分理解为什么,但我没有任何解决方案。

image of the html code inspector html 代码检查器的图像

I included here an image of the html tree I obtain on the web inspector.我在这里包含了我在网络检查器上获得的 html 树的图像。 As one can see, the tree to reach my line of interest gives "/html/frameset/frame 1 / #document /html/body/center/table/tbody/tr[2]/td/a/img".如您所见,到达我感兴趣的行的树给出了“/html/frameset/frame 1 / #document /html/body/center/table/tbody/tr[2]/td/a/img”。 The issue is that I cannot access -by using an xpath- anything that comes after the #document.问题是我无法通过使用 xpath 访问 #document 之后的任何内容。 If I try to copy the xpath of my line of interest, it tracks it back only up to the 2nd "/html".如果我尝试复制我感兴趣的行的 xpath,它只会将其跟踪回第二个“/html”。 It is like the page is subdivided, compartmentalized, with inner parts I cannot access.这就像页面被细分,分隔,内部部分我无法访问。 I suppose there is a way to access the #document content, maybe it refers to another webpage, but I'm stuck at that point.我想有一种方法可以访问 #document 内容,也许它指的是另一个网页,但我当时被卡住了。 I would be very grateful if anyone could help me on this.如果有人可以帮助我,我将不胜感激。

您是否尝试先切换到框架?

driver.switch_to.frame('gauche')

You need to switch control to frame first to handle image on it.您需要先将控制切换到帧以处理其上的图像。 Please see below code for your reference请参阅以下代码供您参考

 driver.switch_to_frame(frame)
 WebDriverWait(driver, 20).until(
 EC.visibility_of_element_located((By.XPATH, "'//img[@src="images/btn_qmj.gif"]'"))).click()
 driver.switch_to_default_content()

Note :: You need to add below imports注意:: 您需要添加以下导入

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

The element is present inside an iframe .In order to access the element you need to switch it first.该元素存在于iframe 。为了访问该元素,您需要先切换它。

Induce WebDriverWait () and frame_to_be_available_and_switch_to_it ()引入WebDriverWait () 和frame_to_be_available_and_switch_to_it ()

Induce WebDriverWait () and visibility_of_element_located()引入WebDriverWait () 和visibility_of_element_located()

WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"gauche")))
WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.XPATH,"//img[@src='images/btn_qmj.gif']"))).click()

You need to import following libraries.您需要导入以下库。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM