【发布时间】:2019-10-22 07:39:02
【问题描述】:
我不确定如何处理这个 selenium/webdriver 异常并使用以下异常行创建一个 try/except/else 语句。我正在抓取的网站有时可能不包含我正在寻找的元素,但我想解决该异常并继续前进。
selenium.common.exceptions.WebDriverException:消息:chrome 无法访问
我在我的 except 语句中尝试了该行的一些变体,但无济于事。
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
我希望能够有一个 try/except/else 语句来打开浏览器,查找该元素,如果它返回一个异常说明该元素存在,那么我希望将它存储到的变量将是这个默认号码。然后继续运行其余代码。
【问题讨论】:
标签: python selenium web-scraping pycharm