单击后使用硒查找元素

时间:2018-09-05 14:36:00

标签: python selenium web-scraping

我无法弄清楚这一点。

我正在研究this site,并且我已经在Python中使用硒来单击类名“ yellow showPopupUnder”下的第一个元素(屏幕的主要部分是关于房屋的20条黄色信息)。

当我让Selenium单击该行后,它将打开并显示更多信息。我对其中有“已选中”和“未选中”框的部分感兴趣。这些复选框位于div中,如下所示:

<div class="v_checked">

未选中的框位于div中,如下所示:

<div class="v_unchecked">

我尝试通过几种方式与他们联系:

driver.find_element_by_class_name('v_checked')
driver.find_element_by_css_selector(".v_checked")
driver.find_element_by_xpath("//div[@class='v_checked']")

我也尝试使用绝对xpath。所有这些都不起作用,我得到一个“ NoSuchElementException:没有这样的元素:无法找到元素”

有人知道如何从这些盒子中检索数据吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

使用了一个iframe,它会打开并在显示这些复选框的位置显示更多信息。您必须导航到该iframe。您可以执行以下操作:

## switch to the iframe ##
driver.switch_to_frame(driver.find_element_by_id("ad_iframe_2_1_1731213"))
## then you can search your checkbox##
driver.find_element_by_class_name('v_checked')
## Switch back to the "default content" (that is, out of the iframes) ##
driver.switch_to_default_content()