简体   繁体   English

使用selenium python按类名查找第n个元素

[英]Find nth element by classname using selenium python

I just started using selenium yesterday to help scrape some data and I'm having a difficult time wrapping my head around the selector engine. 我昨天刚开始使用硒来帮助刮掉一些数据而且我很难将我的脑袋缠绕在选择器引擎上。 I know lxml, BeautifulSoup, jQuery and Sizzle have similar engines. 我知道lxml,BeautifulSoup,jQuery和Sizzle有类似的引擎。 But what I'm trying to do is: 但我想要做的是:

  1. Wait 10 seconds for page to completely load 等待10秒钟以完全加载页面
  2. Make sure there are the presence of ten or more span.eN elements (two load on intitial page load and more after) 确保存在十个或更多span.eN元素(两个负载在初始页面加载后更多)
  3. Then start processing the data with beautifulsoup 然后用beautifulsoup开始处理数据

I am struggling with the selenium conditions of either finding the nth element or locating the specific text that only exists in an nth element. 我正在努力寻找第n个元素或定位仅存在于第n个元素中的特定文本的硒条件。 I keep getting errors (timeout, NoSuchElement, etc) 我一直收到错误(超时,NoSuchElement等)

    url = "http://someajaxiandomain.com/that-injects-html-after-pageload.aspx"
    wd = webdriver.Chrome()
    wd.implicitly_wait(10)
    wd.get(url)
    # what I've tried
    # .find_element_by_xpath("//span[@class='eN'][10]"))
    # .until(EC.text_to_be_present_in_element(By.CSS_SELECTOR, "css=span[class='eN']:contains('foo')"))

You need to understand the concept of Explicit Waits and Expected Conditions to wait for. 您需要了解显式等待和预期条件的概念等待。

In your case, you can write a custom Expected Condition to wait for elements count found by a locator being equal to n : 在您的情况下,您可以编写自定义的预期条件,以等待定位器找到的元素数等于n

from selenium.webdriver.support import expected_conditions as EC

class wait_for_n_elements_to_be_present(object):
    def __init__(self, locator, count):
        self.locator = locator
        self.count = count

    def __call__(self, driver):
        try:
            elements = EC._find_elements(driver, self.locator)
            return len(elements) >= self.count
        except StaleElementReferenceException:
            return False

Usage: 用法:

n = 10  # specify how many elements to wait for

wait = WebDriverWait(driver, 10)
wait.until(wait_for_n_elements_to_be_present((By.CSS_SELECTOR, 'span.eN'), n))

Probably, you could have also just used a built-in Expected Condition such as presence_of_element_located or visibility_of_element_located and wait for a single span.eN element to be present or visible, example: 也许你也可以使用内置的预期条件,例如presence_of_element_locatedvisibility_of_element_located并等待单个span.eN元素出现或可见,例如:

wait = WebDriverWait(driver, 10)
wait.until(presence_of_element_located((By.CSS_SELECTOR, 'span.eN')))

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

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