Exploring the limitations of assigning a variable value in the error function of a tryCatch (R)

As I work on developing a web scraping script, I often encounter errors that require me to handle exceptions. Despite using a tryCatch function to catch these errors, I still see error messages popping up. To suppress these messages, I have used the suppressMessages function. However, even with this in place, I am facing an issue where a new value assigned to a variable upon encountering an error does not seem to stick. This behavior is puzzling me as I am looking for similar functionality to Python's try and except.

The snippet of code causing trouble is outlined below:

tryCatch({suppressMessages(
                {found_university <- css_find(css_list$university)
                google_university <- found_university$getElementText()[[1]]}
              )
      },
          error = function(e){
                found_university <- ""
             })

When an error occurs and the variable found_university has not been defined prior, the following error message is triggered:

Error: object 'found_university' not found

Answer №1

The assigned value remains within the specified function due to R's scoping rules. For more information on this, refer to this resource.

To address this, you can follow @Roland's suggestion and utilize <<- or .GlobalEnv.

Alternatively, you can also try using try:

ans <- try({
    your code
    }, silent=TRUE
)

if(inherits(ans, "try-error")) found_uni <- ""

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips on enhancing SauceLabs javascript queries in your selenium testing for faster speeds?

My experience running Selenium tests on the Chrome browser in SauceLabs has been quite frustrating due to the sluggish performance. One of the major issues I have encountered is the significant delay in javascript queries, taking about 200ms to return res ...

Error encountered with Selenium (Python) due to memory issues

Lately, I have been attempting to scrape data from a waterfall website using Selenium since the request method isn't working and I can't access the API. However, I am facing a challenging issue that seems unsolvable: Memory error. Due to the hi ...

The speed of the local web app tests being conducted with C# Selenium WebDriver is significantly sluggish

Hello fellow developers! I am currently utilizing the latest version of C# Selenium WebDriver to write tests for a web application. Interestingly, the tests function flawlessly when the web app is hosted on a server. However, I encountered significant sl ...

Tips for creating test methods in Python using the Unittest framework

import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys class CorrecaoEfetivaNota(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome('/Users/r13/dev/chromedriver') def teste_login_aval ...

Selenium Remote Driver causes Firefox to crash on Mac OS X when invoked

After setting up the Selenium Remote Driver on my Mac OS X 10.8.5 system and using Firefox 43.0.4, I encountered a problem when executing this code: #!/usr/bin/perl use strict; use warnings; use Selenium::Remote::Driver; use Selenium::Firefox; my $firef ...

Type without any specified XPath location

I've been struggling to log into a website using Python's Selenium because I can't seem to find the xpath or any other method that works. Any suggestions on how I can successfully log in? I've already attempted: DRIVER.find_element_by_ ...

Selenium is having trouble locating a button similar to the one found in Instagram

Hello The issue here is that selenium is unable to locate the like button, as it's not working via xpath and keeps throwing different errors. Click here for the first error Basically, the bot logs into Instagram through posts (based on a global hasht ...

Selenium: a systematic approach to creating test scenarios

Can you share your approach to developing test cases for Selenium 2 / webdriver? In JUnit, developers typically write tests before implementing functionality and continuously run them against the code while making modifications. Is there a more interactive ...

Can you explain the difference between the <li> and <div> elements in HTML? How are they used differently

I'm currently exploring the elements for Selenium usage. I have a question about the <li> tag in HTML - could you explain its significance to me? Also, how does it differ from the <div> tag? ...

Trouble accessing the browser - Session Creation Exception occurred

Here is a problem with the testcase code that I can't seem to get it to execute properly. Is there a configuration issue related to Firefox? package testOperations; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openq ...

Running Selenium tests with Internet Explorer without headless mode enabled

I've been facing an issue with my Python and Selenium script where the function for running IE headless is not working, even though it works perfectly fine for Chrome. I distinctly remember that it used to work before. Any suggestions on what might be ...

The current version of ChromeDriver is incompatible with the installed Chrome browser, which is causing the SessionNotCreatedException: Message: session not created error to occur. Chrome

My current browser is Chrome 75 and I've downloaded the compatible Chromedriver for Linux. I have added it to the PATH variable as directed. However, upon trying to initialize a driver using driver = webdriver.Chrome(), I encountered the following err ...

Script running issue: ChromeDriver from Maven repository not executing properly

Within my pom script, I have included the following dependency: <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.53.1</version> </dependency> ...

Selenium - Struggling to locate the element

Having trouble with clicking a drop-down menu specifically on the second line from the bottom. Selenium keeps throwing an error saying it cannot locate the element I want to click. I've tried using Xpath and ID instead of Class name, but that didn&apo ...

Getting the website value with selenium: A step-by-step guide

Currently, I am coding in JAVA with selenium to automate a website. My goal is to retrieve the value shown in the attached image, which is 40 images. Specifically, I am interested in extracting the value of 40. The HTML structure looks like this: <div ...

Which ExpectedConditions are best suited for me to utilize in order to

When it comes to interacting with WebElements, I usually rely on elementToBeClickable. However, when I need to retrieve text or perform other actions, I have a couple of go-to options that I typically use: presenceOfElementLocated - This checks for the ...

Upgrade to a pop-up window interface add-on using Selenium in Python

https://i.stack.imgur.com/OxI25.png https://i.stack.imgur.com/h5a8k.png https://i.stack.imgur.com/jICQV.png https://i.stack.imgur.com/xnyq6.png Upon clicking the add-on icon, a new "pop-up" or "window" is created, essentially opening another HTML file ...

Extract all table components in Python with Selenium

I need assistance with a webpage that has the following structure: <table class="data" width="100%" cellpadding="0" cellspacing="0"> <tbody> <tr> <th>1</th> ...

Tips for managing the 'connection timed out' error in Selenium WebDriver

While using Selenium Webdriver to access a URL, I encountered a 'connection has timed out' error. Despite this error, my code continued to execute and search for a mail ID without throwing any exceptions. Instead, it displayed the following error ...

Tips for moving and saving a file to a specific location before downloading it using Python and Selenium with the Chrome Webdriver

Recently, I started using the Selenium Chrome WebDriver. I have been navigating a webpage where I input my login details and then download a file in CSV format by clicking on a button (the file is saved directly to my downloads folder). However, I would p ...