user17853846
user17853846

Reputation:

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);

Upvotes: 2

Views: 3104

Answers (1)

Max Daroshchanka
Max Daroshchanka

Reputation: 2968

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)

Upvotes: 2

Related Questions