Executing jQuery inside Selenium

Viewed 27

I'm using webbrowser to crawl some pages and am most comfortable / familiar with jQuery & CSS selectors to navigate the DOM.

I was trying to read around but wasn't sure if this is possible, or will do what I want it.

In the console I can run something like:

x = $('.job-container').children()

and it will return me the list of children. I can then scroll through them and get what I'm looking for.

I tried this in Selenium

from selenium import webdriver
browser = webdriver.Chrome()
url = "some_url"
browser.get(url)
browser.execute_script("$('.job-container').children()[0]")

However its not returning anything.

I'm less familiar with how to navigate the DOM using xPath, so I thought I'd try jquery w/ CSS selectors first and if that doesn't work, try to pick up xpath.

Is there a way to use jQuery inside execute_script? Or will it not do what I want it to do?

1 Answers

Try to execute something like

browser.execute_script("return $('.job-container').children()[0]")

or

browser.execute_script("return document.querySelector('.job-container').childNodes[0]")
Related