I am not sure how to take this selenium/webdriver exception and create a try/except/else statement with the following exception line. The website im scraping sometimes may not contain the element im looking for but I would like to address that exception and move on.
selenium.common.exceptions.WebDriverException: Message: chrome not reachable
Ive tried some variations of that line in my except statement to no avail.
def planCosts():
driver.get("https://shop.freedommobile.ca/devices/Samsung/Galaxy_S10+?sku=887276301570&planSku=Freedom%20250MB")
MSRP = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.cKlhCz')))
MSRP = MSRP[0].text
MSRP = re.findall(r'\d+', MSRP)
MSRP = int(MSRP[0])
print(MSRP)
# grabbing the lowest upfront payment from string of min and max
try: # checks to see if element exist
upfrontPaymentRaw = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '.col-lg-6 .fTfebt')))
upfrontPayment = upfrontPaymentRaw[0].text
except selenium.common.exceptions.WebDriverException: #if I get an error looking for it then just make it default number above(MSRP)
myTabCharge = MSRP
else: #if no error run this code
upfrontPayment = re.findall(r'\d+', upfrontPayment)
upfrontPaymentLowest = int(upfrontPayment[0])
upfrontPaymentHighest = int(upfrontPayment[1])
myTabCharge = (upfrontPaymentHighest - upfrontPaymentLowest) / 24
im hoping to be able to have a try/except/else statement that opens the browser, looks for that element, if it returns an exception that states the element is there then the variable i was looking to store it into will be this default number. Then continue running the rest of the code.
You'll need that exception type in your namespace in order to catch it, so add to your imports:
from selenium.common.exceptions import WebDriverException
And then try changing your except statement to:
except WebDriverException as e:
if e.msg.strip().endswith("chrome not reachable"):
myTabCharge = MSRP
else:
raise
That way you're only catching the specific exceptions you want to bypass, and raising anything else.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.