ajax - Find nth element by classname using selenium python -


i started using selenium yesterday scrape data , i'm having difficult time wrapping head around selector engine. know lxml, beautifulsoup, jquery , sizzle have similar engines. i'm trying is:

  1. wait 10 seconds page load
  2. make sure there presence of ten or more span.en elements (two load on intitial page load , more after)
  3. then start processing data beautifulsoup

i struggling selenium conditions of either finding nth element or locating specific text exists in nth element. keep getting errors (timeout, nosuchelement, etc)

    url = "http://someajaxiandomain.com/that-injects-html-after-pageload.aspx"     wd = webdriver.chrome()     wd.implicitly_wait(10)     wd.get(url)     # 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 understand concept of explicit waits , expected conditions wait for.

in case, can write custom expected condition wait elements count found locator being equal n:

from selenium.webdriver.support import expected_conditions 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 wait  wait = webdriverwait(driver, 10) wait.until(wait_for_n_elements_to_be_present((by.css_selector, 'span.en'), n)) 

probably, have used built-in expected condition such presence_of_element_located or visibility_of_element_located , wait single span.en element present or visible, example:

wait = webdriverwait(driver, 10) wait.until(presence_of_element_located((by.css_selector, 'span.en'))) 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -