How would I make my code reload instead of crashing after an error in python selenium chromedriver? - selenium

I have it as wait until clickable so I can keep reloading on my own but i want it to just do it on it's own because constantly reloading isn't fun and is a waste of time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get('link.com')
wait = WebDriverWait(driver, 1000)
AddToKart = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="content"]/div/div/div[1]/div[3]/div[1]/section[2]/section/div[15]/div/div[2]/button'))).click()```

wait = WebDriverWait(driver, 5)
count = 1
while(count==1):
count=0
try:
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="content"]/div/div/div[1]/div[3]/div[1]/section[2]/section/div[15]/div/div[2]/button'))).click()
except Exception as e:
print(e)
count=1
try:
driver.refresh()
except Exception as e:
print(e)
Just wrapp it with while and try/except

Related

Unable to locate element of save button

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.common.exceptions import NoSuchElementException,TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
driver.maximize_window()
#driver.implicitly_wait(100)
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
element=driver.find_element("id",'btnSave')
time.sleep(5)
element.click()
driver.quit()
Nothing happen when i locate element of save button. Rest of the code is working correct. I have tried all most everything but still locators not working.

Automating web application in selenium with python

This is my code where all things are alright but at last when i click on save button its not working. I have tried accessing element by id, name, xpath and class name of the save button but click for save not working. When that part need to executed nothing happen.Rest of the program executing properly.Plz suggest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
time.sleep(5)
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
#time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
#time.sleep(5)
driver.find_element("class name",'RadButton RadButton_Hay rbLinkButton btn_new_bg_popUpnew').click()
#time.sleep(5)
element=driver.find_element("xpath",'//*[#id="btnSave"]').click()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import time
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
driver = webdriver.Chrome(executable_path = r"C:\Users\mvintee\project_for_selenium\chromedriver_win32\chromedriver.exe")
actions = ActionChains(driver)
driver.get("http://pis033/NorthStarWMS/User/PurchaseOrders.aspx?t=1&PO=1")
print(driver.title)
driver.find_element("id","Login1_UserName").send_keys("wms")
driver.find_element("id","Login1_Password").send_keys("1")
driver.find_element("class name","rbText").click()
time.sleep(5)
element_to_hover_over=driver.find_element("xpath",'//*[#id="ctl00_MenuTabNew"]/ul/li[4]/span/span')
time.sleep(5)
hover = ActionChains(driver).move_to_element(element_to_hover_over)
hover.perform()
time.sleep(5)
driver.find_element("xpath", '//*[#id="ctl00_MenuTabNew"]/ul/li[4]/div/ul/li[1]/a/span').click()
#time.sleep(5)
driver.find_element("id","ctl00_ContentPlaceHolder1_ShipOrderGrid_ctl00_ctl02_ctl00_btnAdd").click()
#time.sleep(5)
driver.find_element("class name",'RadButton RadButton_Hay rbLinkButton btn_new_bg_popUpnew').click()
#time.sleep(5)
element=driver.find_element("xpath",'//*[#id="btnSave"]').click()

Selenium: selenium.common.exceptions.TimeoutException: Message: error

I was trying to run this script to automate and click on the product list in the webpage using selenium. But this "raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message: error" is occurring every time. What is wrong I'm doing here? Expecting your guidance.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
chrome_options.add_argument('__headless')
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')
product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[#class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()
time.sleep(10)
driver.close()
output
PS G:\Python_Practice\scrapy_practice\test> [21628:13792:0911/125833.339:ERROR:gpu_init.cc(441)] Passthrough is not supported, GL is disabled
> & C:/Users/raisu/anaconda3/envs/Scrapy_Workspace2/python.exe g:/Python_Practice/scrapy_practice/test/test.py
DevTools listening on ws://127.0.0.1:56456/devtools/browser/1d6d20ce-ecb9-44f7-be6e-1dbe1373526a
Traceback (most recent call last):
File "g:/Python_Practice/scrapy_practice/test/test.py", line 18, in <module>
product_tab = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//article[#class='panelProduct panelLayout_panelLayout__BDQ6_ view_product__3AOqY']/a"))).click()
File "C:\Users\raisu\anaconda3\envs\Scrapy_Workspace2\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Instead of using the xpath, I've used a CSS_SELECTOR and also I changed the wait condition for element to be visible
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from shutil import which
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
chrome_options = Options()
chrome_options.add_argument('__headless')
chrome_path = which('chromedriver')
driver = webdriver.Chrome(executable_path=chrome_path, options=chrome_options)
driver.set_window_size(1920, 1080)
driver.get('https://www.galaxus.ch/search?q=5010533606001')
#time.sleep(5) use this only if the wait is not working
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'article > a')))
driver.find_element(By.CSS_SELECTOR,'article > a').click()
driver.close()

Xpath grabbing wrong text

I'm trying to grab the price from this page: https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq
It should be getting $1,376.15 CAD however I'm getting some other text from the page and sometimes it doesn't work at all and gives me:
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Here's my code:
DRIVER_PATH = '/usr/bin/chromedriver'
options = webdriver.ChromeOptions()
options.add_argument("--headless")
driver = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
XPATH = '//*[#id="app"]/main/div/div/section[1]/div/div[3]/div/span[1]'
url = 'https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq'
driver.get(url)
price = WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, XPATH)))
print(price)
driver.quit()
Try that out:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.support import expected_conditions as EC
DRIVER_PATH = '/usr/bin/chromedriver'
options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path=DRIVER_PATH, options=options)
url = "https://www.eq3.com/ca/en/product/cjv8cke45026q01786nahx8uf/lighting/lighting/pendant-lamps/nelson-bell-bubble-pendant-lamp?cjv49km02036401865sece5be=cjv49km0503650186hc90zvnq"
driver.get(url)
el = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(#class,'MuiTypography-root') and contains(#class,'MuiTypography-h3')][1]")))
text = el.text
print(text)
driver.quit()

Rum multiple test cases using python selenium webdriver on same web browser session

I have following code here:
import unittest
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
class MyTest1(unittest.TestCase):
#classmethod
def setUpClass(cls):
cls.driver = webdriver.Firefox()
driver = cls.driver
driver.get("https://somewebsite.com")
print "login the website"
def test_UI_login(self):
driver = self.driver
print "test some things here"
def test_duplicate_client(self):
driver = self.driver
print "test some things here"
def tearDown(cls):
cls.driver.close()
if __name__ == '__main__':
unittest.main()
Problem I am facing is ,
After first function test_UI_login, the firefox instance closes. How can I execute multiple test cases from the same Firefox instance in unittest using selenium.
As stated in this SO-answer
you must initialize the driver in __init__ .
Assign the webdriver in __init__(), but don't forget to call super.__init__() to not break the tests mechanism:
def __init__(self, *args):
super().__init__(*args)
self.driver = webdriver.Firefox()