硒提取问题:等待/未找到元素 [英] Selenium Extraction Problems: Waits/Not Finding Elements

查看:199
本文介绍了硒提取问题:等待/未找到元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在chrome和firefox中,一切都很好,直到我需要提取文本。我得到这个错误:

In both chrome and firefox, everything is fine up until I need to extract text. I get this error:

h3 = next(element for element in h3s if element.is_displayed())
StopIteration

我甚至添加了流畅的等待。

I even added a fluent wait.

browser = webdriver.Firefox()
browser.get('https://www.voilanorbert.com/')
inputElement = browser.find_element_by_id("form-search-name")
inputElement.send_keys(leadslist[i][0])
inputElement = browser.find_element_by_id("form-search-domain")
inputElement.send_keys(leadslist[i][1])
searchbutton = browser.find_element_by_name("search")
searchbutton.click()

wait = WebDriverWait(browser, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.results")))
wait2 = WebDriverWait(browser, 3000, poll_frequency=100, ignored_exceptions=[ElementNotVisibleException])
wait2.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "h3.one")))
h3s = browser.find_elements_by_css_selector('h3.one')
h3 = next(element for element in h3s if element.is_displayed())
result = h3.text

,所以它只是一个空的列表。
一些图片可能会有帮助:
这是前面的图片:

I think its because its not actually extracting anything, so its just an empty list. Some pictures that will probably help: This is the before picture:

这是后图:

This is the after picture:

我需要提取什么是文本中心显示类的结果类。

/ p>

推荐答案

答案很简单,你只需要一个不同的选择器,等待搜索结果。
下面的方法(C#)工作得很好,它会减少你的代码与几行。

The answer is fairly simple, you just need a different selector when waiting for the search result. The approach below (C#) works perfectly, it'll reduce your code with a few lines.

一个结果DIV完成。它是唯一具有文本中心显示类的元素,所以这是所有的选择器需要。
一旦显示这样的DIV,你知道在哪里精确定位H3元素(它是所述DIV的子元素)。
因此,只需等待以下元素在您点击搜索按钮后可见:

One "result DIV" becomes visible when the search is done. It is the only element with the "text-center displayed" class, so that's all your selector needs. Once such a DIV is displayed, you know where to pinpoint the H3 element (it's a child of said DIV). So simply wait for the below element to become visible after you've clicked the search button:

        IWebElement headerResult = w.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("div[class=\"text-center displayed\"] h3")));
        string result = headerResult.Text;

这篇关于硒提取问题:等待/未找到元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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