How to wait until element is available in selenium python

Refresh

November 2018

Views

1.5k time

2

I am writing script using selenium python but there is problem i have tried to find solution but i can not find one that was helpful to me. here is the code

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

class sulekhastart(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_parse_contact_urls_and_go_to_next_page(self):

        pagenumber = 'Page'

        #assign WEBDRIVER to local webdriver
        driver = self.driver

        #Website open by below url
        driver.get("http://www.sulekha.com/ac-dealers/bangalore")
        self.assertIn("Sulekha Bangalore", driver.title)

        #close the lightbox thnat appears at the firsttime load of page
        startlightbox = driver.find_element_by_xpath('//a[@class="lcf-close"]')
        startlightbox.click()

        while True:

            #get the page number
            pageno = driver.find_element_by_xpath('//li[@id="numberPage"]/strong')
            print pageno.text
            print pagenumber

            #check if page same as last page or not
            if str(pageno.text) != pagenumber:

                pagenumber = str(pageno.text)

                businessname = driver.find_elements_by_xpath('//li/div/div[@class="busi-name"]/h3/a')
                records = len(businessname)

                #print all data that are available on the webpage
                for i in range(0,records):
                    print businessname[i].get_attribute('href')
                    print businessname[i].text

                nextpage = driver.find_element_by_xpath('//li[@id="nextPage"]')
                nextpage.click()
            else:
                print 'This is last page all data is scraped change url and get another data'
                break

            element = WebDriverWait(driver, 10).until_not(EC.presence_of_element_located((By.XPATH, "/html/body/div/div/svg")))

    def tearDown(self):
        self.driver.close()
        print 'page not be closed'

if __name__ == "__main__":
    unittest.main()

and i want to wait script after click on the next button until By.XPATH, "/html/body/div/div/svg" this element gone from DOM or page source and then after wait until 3 seconds

1 answers

0

а Андерссон прокомментировал

замена

element = WebDriverWait(driver, 10).until_not(
            EC.presence_of_element_located((
              By.XPATH, "/html/body/div/div/svg")))

с

element = WebDriverWait(driver, 10).until_not(
            EC.presence_of_element_located((
              By.XPATH, "/html/body/div/div/*[name()='svg']")))

решает проблему