Home > Back-end >  Driver get within loop repeats opening chrome
Driver get within loop repeats opening chrome

Time:02-04

How to reduce number of selenium running google chrome while getting data. Precisely, I have this code in for which iterate more than 200 times:

try:
    DRIVER_PATH = '/home/Selenium-drivers/chromedriver'
    driver = webdriver.Chrome(executable_path=DRIVER_PATH)
    driver.get(url_specific_wine)
    regions = driver.find_element(By.XPATH, '//*[@id="MainContent"]/div[2]/div[4]/div/div/div[5]/div[4]/div/div/div[9]/div[2]').text

except:
    regions = ''
    all_regions.append(regions)

That will open a google chrome with each time. How to avoid that?

CodePudding user response:

This your current code can be improved at least in 2 points:
1.

DRIVER_PATH = '/home/Selenium-drivers/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

Should be taken out of the loop.
2.
You should use Expected Conditions explicit waits, something like this:

DRIVER_PATH = '/home/Selenium-drivers/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)
wait = WebDriverWait(driver, 20)
try:
    driver.get(url_specific_wine)
    regions = wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@id="MainContent"]/div[2]/div[4]/div/div/div[5]/div[4]/div/div/div[9]/div[2]'))).text
except:
    regions = ''
    all_regions.append(regions)

//*[@id="MainContent"]/div[2]/div[4]/div/div/div[5]/div[4]/div/div/div[9]/div[2] XPath locator seems to be not reliable. I have no idea what element are you trying to locate, by the locator have to be improved here.
In order to use wait object above you will need the following imports

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
  •  Tags:  
  • Related