Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting the timeout period for Selenium FindElement()

How can I limit/reduce the timeout period for FindElement? I am scraping a website. For a table which appears in thousands of pages, I can have either an element stating there is no information, or the table. I search for one of theses elements and when missing, I search for the other. The problem is that when one of them does not exist, it takes a long time until the FindElement times out. Can this period be shortened? Can the timeout period be defined per element? All I found about waits are to prolong the timeout period... I'm working in a .NET environment, if that helps.

like image 415
Asaf Avatar asked Apr 06 '17 06:04

Asaf


People also ask

What is findElement () in Selenium?

findElement method in Selenium is a command which helps you identify a web element. There are multiple ways that findElement provides to uniquely identify a web element within the web page using web locators in Selenium like ID, Name, Class Name, Link Text, Partial Link Text, Tag, which we will see later in the blog.

What is driver manage () timeouts () implicitlyWait?

1. implicitlyWait() This timeout is used to specify the amount of time the driver should wait while searching for an element if it is not immediately present.

How do I change the default timeout in Selenium?

The default value of timeout is 0. This method is generally used for JavaScript commands in Selenium. If we omit setting time for the script, the executeAsyncScript method can encounter failure due to the more time consumed by the JavaScript to complete execution.


Video Answer


1 Answers

The delay in FindElement is caused by the Implicit Wait settings. You can set it temporary to different value

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(0)); // setting to 0 will check one time only when using FindElement

// look for the elements

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(original settings));
like image 87
Guy Avatar answered Sep 22 '22 22:09

Guy