Launch one instance of selenium or helium driver

198 Views Asked by At

I am trying to download some images from a website (The image is changeable) and I have used the following code

from helium import *

for i in range(3):
    driver = start_chrome("https://eservices.moj.gov.kw/searchPages/searchCases.jsp", headless=True)
    element = driver.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
    driver.execute_script("arguments[0].scrollIntoView();", element)
    element.screenshot(f'Number_{i}.png')

The code in each loop start new driver and I think this is slow somewhat. How can I be able to launch the driver for just once then to naviagte to the url several times?

I tried this

from helium import *

driver = start_chrome("https://eservices.moj.gov.kw/searchPages/searchCases.jsp", headless=True)

for i in range(3):
    driver.get("https://eservices.moj.gov.kw/searchPages/searchCases.jsp")
    element = driver.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
    driver.execute_script("arguments[0].scrollIntoView();", element)
    element.screenshot(f'Number_{i}.png')

This worked well but I am not sure if I am right of using the same url to start_chrome and then using the url again with the get method.

1

There are 1 best solutions below

2
PDHide On BEST ANSWER
from helium import *

driver =start_chrome()
for i in range(3):
    driver.get(
        "https://eservices.moj.gov.kw/searchPages/searchCases.jsp")
    element = driver.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
    driver.execute_script("arguments[0].scrollIntoView();", element)
    element.screenshot(f'Number_{i}.png')

or

from helium import *

driver = start_chrome(
    "https://eservices.moj.gov.kw/searchPages/searchCases.jsp")
for i in range(3):
    driver.refresh()
    element = driver.find_element_by_xpath("//*[@id='frmCaseNo']/div[2]/img")
    driver.execute_script("arguments[0].scrollIntoView();", element)
    element.screenshot(f'Number_{i}.png')

You can use the above approach to avoid opening url twice