Reputation: 1695
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?
Upvotes: 1
Views: 503
Reputation: 601
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.
Upvotes: 0
Reputation: 1525
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.
Upvotes: 1