Home > Mobile >  If statement throws error instead of moving to next line
If statement throws error instead of moving to next line

Time:08-24

I'm trying to detect a popup that doesn't always occur using an If statement where if the element is found click the no thanks option else next function

import time
import subprocess
import pyperclip
import pyautogui
import re
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

serv = Service(r"C:\Users\Batman\Desktop\Driver\chromedriver.exe")
driver = webdriver.Chrome(service=serv)
driver.get("https://www.dell.com/support/home/en-us/product-support/servicetag/0-UG15dVNvUU9sTVVLa2ovUFAvZmh4QT090/overview")
driver.maximize_window


def FeedBack():
#popup asking for feedback
    if driver.find_element(By.TAG_NAME,("noButton.buttons")):
        driver.find_element(By.TAG_NAME, "noButton.buttons").click()
    else:
        DriversAndDownloads()
FeedBack()

The Full code

import time
import subprocess
import pyperclip
import pyautogui
import re
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys


serv = Service(r"C:\Users\Batman\Desktop\Batman\Pycharm\DellUpdate - selenium\Driver\chromedriver.exe")
driver = webdriver.Chrome(service=serv)
driver.get("https://www.dell.com/support")
driver.maximize_window()



def Step1():
    SearchBar = driver.find_element(By.NAME, "entry-main-input-home").send_keys("3Q84KQ2")
    SearchBarClick = driver.find_element(By.ID,"txtSearchEs").click()
Step1()


def Popup():
#30 second popup
    if driver.find_element(By.ID, "sec-overlay"):
        time.sleep(30.5)
        SearchBarClick = driver.find_element(By.ID, "txtSearchEs").click()
    else: FeedBack()
Popup()


def FeedBack():
#popup asking for feedback
    if driver.find_element(By.TAG_NAME,("noButton.buttons")):
        driver.find_element(By.TAG_NAME, "noButton.buttons").click()
    else:
        DriversAndDownloads()
FeedBack()

def DriversAndDownloads():
    time.sleep(3)
    driver.find_element(By.ID, "drivers").click()
DriversAndDownloads()


def FindDrivers():
    driver.find_element(By.CSS_SELECTOR, "btn.collapse-toggle.table-collapse-toggle.collapsed").click()
FindDrivers()

The difference between the two above codes is the first will take you to where the popup "should" randomly comes up. (I tested a few times and it didn't seem to popup once from the link directly to that page) which was a great way to play with the IF statement

the Second code is the full code (minus the function that gets the Dell serial number, I've provided a working serial to test with).

Final note- I did find out that I can just add a feature to refresh the page(f5) and it'll skip the popup every time upon reloading. The only problem with this is it doesn't help me figure out the code for what I assume is written correctly based on my desired result.

CodePudding user response:

driver.find_element will throw exception if no element found.
You should use driver.find_elements instead.
It will return a list. In case of element existence it will return a non-empty list enterpreted by Python as True. Otherwise it will return an empty list interpreted by Python as Boolean False.
So, instead of

if driver.find_element(By.TAG_NAME,("noButton.buttons")):
    driver.find_element(By.TAG_NAME, "noButton.buttons").click()
else:
    DriversAndDownloads()

Try using

if driver.find_elements(By.TAG_NAME,("noButton.buttons")):
    driver.find_element(By.TAG_NAME, "noButton.buttons").click()
else:
    DriversAndDownloads()

CodePudding user response:

if driver.find_element(By.TAG_NAME,("noButton.buttons")) won't work in case element not found since it doesn't return boolean True/False but WebElement instance or NoSuchElementException. You need either do

if driver.find_elements(By.TAG_NAME,("noButton.buttons")):
    # do something
else:
    # do something else

or

from selenium.common.exceptions import NoSuchElementException

try:
    driver.find_element(By.TAG_NAME,("noButton.buttons"))
    # do something
except NoSuchElementException:
    # do something else
  • Related