Home > Software design >  Python Selenium - Using Try and except in a loop for automated reading on a website
Python Selenium - Using Try and except in a loop for automated reading on a website

Time:10-11

I'm trying to create an automatic soundcloun player. I started learning selenium on python a few days ago, I'm still a beginner. So I would like to create a loop that works when he enters the page he clicks on "I accept" (For cookies) then presses the space button and refreshes after 60s

Here is the code:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from typing import KeysView
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome(executable_path="chromedriver.exe")

driver.implicitly_wait(17)

driver.get("https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjkk_fNxM_6AhXrgc4BHWcSCjoQFnoECAYQAQ&url=https://soundcloud.com/user-997915825/pmh-ah-aah&usg=AOvVaw2vFzZHA4d8DH1r5GjZ6e4y")

try:
element = driver.find_element(By.ID, 'onetrust-accept-btn-handler')

action = ActionChains(driver)

action.click(on_element = element)

action.perform()

driver.implicitly_wait(33)

except NoSuchElementException:
ActionChains(driver).key_down(Keys.SPACE).key_up(Keys.SPACE).perform()


while True:
   sleep(60)
   driver.refresh()

But when I put try: then Except: NoSuchElementException I don't even have time to decompile that I get an error in the lines like : Try statement must have at least one except or finally clause. Pylance [Ln 17] Expected expression. Pylance [Ln 28]

I used the right libraries I think:

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.common.exceptions import NoSuchElementException

And finally when I try to run it I have this nice error : SyntaxError: expected 'except' or 'finally' block.

When I remove try and except as follows:

 from selenium import webdriver
 from time import sleep
 from selenium.webdriver.common.action_chains import ActionChains
 from selenium.webdriver.common.by import By
 from selenium.webdriver.support.ui import WebDriverWait
 from selenium.webdriver.support import expected_conditions as EC
 from typing import KeysView
 from selenium.webdriver.common.keys import Keys
 from selenium.common.exceptions import NoSuchElementException

 driver = webdriver.Chrome(executable_path="chromedriver.exe")

 driver.implicitly_wait(17)

 driver.get("https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjkk_fNxM_6AhXrgc4BHWcSCjoQFnoECAYQAQ&url=https://soundcloud.com/user-997915825/pmh-ah-aah&usg=AOvVaw2vFzZHA4d8DH1r5GjZ6e4y")

 element = driver.find_element(By.ID, 'onetrust-accept-btn-handler')

 action = ActionChains(driver)

 action.click(on_element = element)

 action.perform()

 driver.implicitly_wait(33)

 ActionChains(driver).key_down(Keys.SPACE).key_up(Keys.SPACE).perform()


 while True:
     sleep(60)
     driver.refresh()

He enters the page, he clicks on "I accept" and then he presses the SPACE button to start reading but when he refreshes, since he doesn't have a "cookie acceptance" window anymore, he doesn't even start reading. that's why I'm looking to use conditions. I've searched several threads about this, I tried to understand their code before copying it but so far all the methods I've tried have failed and it's already been 2 days constantly that it's blocking me, my eyes are starting to hurt. If anyone can find a solution to my problem, I would be delighted I'll be glad and can find the problem or I'm the problem.

Thanks for your correction, Prophet, here is the new code :

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from typing import KeysView
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

driver = webdriver.Chrome(executable_path="chromedriver.exe")

driver.get("https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjkk_fNxM_6AhXrgc4BHWcSCjoQFnoECAYQAQ&url=https://soundcloud.com/user-997915825/pmh-ah-aah&usg=AOvVaw2vFzZHA4d8DH1r5GjZ6e4y")

try:
    element = driver.find_element(By.ID, 'onetrust-accept-btn-handler')

    action = ActionChains(driver)

    action.click(on_element = element)

    action.perform()

except NoSuchElementException:
  ActionChains(driver).key_down(Keys.SPACE).key_up(Keys.SPACE).perform()

while True:
    sleep(7)
    driver.refresh()

CodePudding user response:

You are missing indentation.
This is a very basic python syntax rule.
The code inside try or except or any other block should have indentation relatively to previous code.
With the indentation your code could look like this:

driver = webdriver.Chrome(executable_path="chromedriver.exe")

driver.implicitly_wait(17)

driver.get("https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjkk_fNxM_6AhXrgc4BHWcSCjoQFnoECAYQAQ&url=https://soundcloud.com/user-997915825/pmh-ah-aah&usg=AOvVaw2vFzZHA4d8DH1r5GjZ6e4y")

try:
    element = driver.find_element(By.ID, 'onetrust-accept-btn-handler')

    action = ActionChains(driver)

    action.click(on_element = element)

    action.perform()

    driver.implicitly_wait(33)

except NoSuchElementException:
    ActionChains(driver).key_down(Keys.SPACE).key_up(Keys.SPACE).perform()

Also, driver.implicitly_wait(33) is not a pause command.
driver.implicitly_wait is set once per the session. Is sets the timeout, how long time to try finding (pooling) an element on the page.
If you want to make a pause time.sleep() should be used, but not recommended since we don't generally recommend to use hardcoded sleeps in Selenium code.

  • Related