import selenium
import time
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options=webdriver.ChromeOptions()
PATH="C:\Coding_projects\chromedriver.exe"
class AppleWebsite():
def MAC():
driver=webdriver.Chrome(PATH,options=options)
driver.get("https://www.apple.com/fr/iphone-12/")
MAC=driver.find_element_by_xpath('//*[@id="ac-globalnav"]/div/ul[2]/li[3]/a')
MAC.click()
time.sleep(random.randint(2,9))
MAC_AIR=driver.find_element_by_xpath('//*[@id="chapternav"]/div/ul/li[2]/a')
MAC_AIR.click()
AppleWebsite.MAC()
Hello, I have my selenium script in a function everything works fine but the browser tab closes as soon as the script is finished even though I don’t have a driver.quit() any idea on to solve this ?
CodePudding user response:
Selenium always automatically quits after a code is finished running. You can add a time.sleep() to keep it open. Like this:
import selenium
import time
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
options=webdriver.ChromeOptions()
PATH="C:\Coding_projects\chromedriver.exe"
class AppleWebsite():
def MAC():
driver=webdriver.Chrome(PATH,options=options)
driver.get("https://www.apple.com/fr/iphone-12/")
MAC=driver.find_element_by_xpath('//*[@id="ac-globalnav"]/div/ul[2]/li[3]/a')
MAC.click()
time.sleep(random.randint(2,9))
MAC_AIR=driver.find_element_by_xpath('//*[@id="chapternav"]/div/ul/li[2]/a')
MAC_AIR.click()
AppleWebsite.MAC()
time.sleep(20)