1

I'm using Python/Selenium with the Chrome webdriver, and I'm trying to retrieve a url from one <td> based on the content of another <td>. My markup looks like:

<div class="targetclass">
    <tr>
       <td><a href="[email protected]">emailval2</a></td>
       <td><a href="[email protected]">emailval</a></td>
    </tr>
</div>

That's easy enough with jQuery and script executor:

with open('jquery-3.2.1.min.js', 'r') as jquery_js: 
    jquery = jquery_js.read() #read the jquery from a file
    driver.execute_script(jquery) # activate the jquery lib
    driver.execute_script("$('div.targetclass a[href$=\"[email protected]\"]').parents(\"tr\").find(\"a:first\").attr('href')")

However, when I try to store the returned href to use with webdriver, I have the following result:

aurlval = driver.execute_script("$('div.targetclass a[href$=\"[email protected]\"]').parents(\"tr\").find(\"a:first\").attr('href')")
print (aurlval)

The returned value is

None

How can I store the target url ([email protected]) so that I can manipulate it with the webdriver?

2 Answers 2

1

My experience with Selenium is limited to some niche cases where I wanted some automation (for scraping I can normally get by with requests and BeautifulSoup) but I believe the reason you are getting None is because execute_script doesn't return a value to begin with (your script is basically just being injected into the webpage and executed within the browser). Iirc, you should be able to parse your jquery out to (verbosely):

div = driver.find_element_by_class_name("targetclass")
targeta = div.find_element_by_link_text("[email protected]")
tr = targeta.parent.parent
retrieve = tr.find_element_by_tag_name("a")
aurlval = retrieve.getattribute("href")

I can't recall of the top of my head if Selenium has separate methods for list vs first-element, so you may have to take the zero index on those lines.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm hitting a roadblock at the .parent line, and I think it's because .parent is used differently in Selenium. It looks like I may need xpath to achieve this.
Selenium does have separate methods for list vs first elements. You just make it plural, e.g., find_elements_by_...
0

One way is to use javascript as below to get the table row along with the previous answers steps. WebDriver doesn't have a 'parent' method of its own.

div = driver.find_element_by_class_name("targetclass")
targeta = div.find_element_by_link_text("[email protected]")
tr = self.driver.execute_script("return arguments[0].parentNode.parentNode;", targeta)

Then you can find elements in the row.

Or you can try "return arguments[0].parentNode;" which should get the td and then try to get previous-sibling, but I haven't tried that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.