Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Selenium--driver.execute_script() wont work

I execute a javascript with driver.execute_script but NOTHING occurs when I do--the system simply goes to the next python code line

Thoughts? webscraping in webpage--using JavaScript in Console to datamine. The jScript works wonderfully when I put it to the webconsole

JavasCRIPT:

let email = '';
let contacts = document.querySelectorAll('div.contact-section');
for (let i = 0; i < contacts.length; i++) {
  if (contacts[i].getAttribute('automation-id') === 'contact-true-owner') {
    ele = contacts[i]
  }
}
let links = ele.querySelectorAll('address > a')
let last = links.length - 1;
email = links[last].innerText;
console.log(email);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Add return to script

This script actually do nothing you could detect from python code. The last line doesn't return anything..

Try to add the return statement:

...
email = links[last].innerText;
console.log(email);
return email

And also, it looks like the job you trying to do with driver.execute_script is just to locate the element and call .innerText. I gess it could be solved by providing the right xpath query (maybe css selector also OK, but xpath should work exactly).

I mean there is more strait-forward way to solve this issue, not just delegate it to for loop in driver.execute_script. But it's on your choice.

I'll be able to help you with xpath if you share the page-source. But it's optional.

With xpath

The idea is to perform 2 steps:

1 Find the element by xpath (other selector).

2 Pass it to executeScript as argument.

element = driver.find_element_by_xpath("//div[contains(@class, 'contact-section')][@automation-id='contact-true-owner']//address/a[last()]")

text = driver.execute_script('return arguments[0].innerText;', element)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!