Skip to content
Advertisement

Selenium bug: Message: invalid argument: ‘url’ must be a string

I have some simple selenium scraping code that returns all the search results, but when I run the for loop, it displays an error: Message: invalid argument: ‘url’ must be a string

(Session info: chrome=93.0.4577.82)

JavaScript

I would like to ask for some help. How to avoid this error? Thanks.

Advertisement

Answer

You are trying to get the “user_data”

JavaScript

immediately after opening the YouTube url

JavaScript

This causes “user_data” to be an empty list.
This is why when you trying to iterate over “links” with

JavaScript

to iterate over single “x” value of “NoneType” object, not a string.
To fix this you should add a wait/ delay between

JavaScript

and

JavaScript

The simplest way to do that is to add a delay there, like this:

JavaScript

However the recommended approach is to use explicit wait implemented by expected conditions, like this:

JavaScript

Also, you should use visibility_of_element_located instead of presence_of_element_located since presence_of_element_located waits only for element initial presence, element state while it’s content like texts etc. may still not be ready.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement