Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

python - How to close Webdriver session without stopping script entirely?

I'm trying to write a code that will go into a website, click a few buttons and check a status.

If that status is negative, it should end the WebDriver session and open a new one.

I've tried with driver.quit() and driver.close(), but no matter what I do, after the first session ends, the script dies, and I can't run it again unless I start over manually.

Here is what I've got at the moment:

def attempt():

  options = webdriver.ChromeOptions()

  options.add_argument("start-maximized")

  driver = webdriver.Chrome(chrome_options=options, executable_path=mypath) #my path is an actual 
                                                                                          #path 

  driver.get(website) #website is an actual website
  
  ... # Here goes all the 

  ... # button-clicking in the middle
  
  if "negative" in driver.page_source:

  driver.quit() #or driver.close()

  else:

  print("success")

while keyboard.is_pressed(';') != True:

  attempt()

The code is simplified here, but it works as intended when running it once. Once it ends and the first session closes, the script just stops running, instead of running again. I'm not sure if this has a workaround, and I haven't been able to find one around, so I decided to ask here. Sorry if code looks a bit wonky, it's my first time using the tool


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...