python selenium how to get link from google
<p>How do I get a link from Google? I tried the following ways, but none of them worked:</p>

<ol>
<li><code>find_elements_by_xpath("//*[@class='r']/@href")</code></li>
<li><code>driver.find_element_by_xpath("//a").get_attribute("href")</code></li>
<li><code>driver.find_element_by_xpath("//a").get_attribute(("href")[2])</code></li>
</ol>

<p>I am receiving "none"</p>

<p>I need to get google link, not link to the site (e.g. not www.stackoverflow.com). It's highlighted on this image:</p>

<p><a href="http://i.stack.imgur.com/RGpo6.png" rel="nofollow"><img src="http://i.stack.imgur.com/RGpo6.png" alt="enter image description here"></a></p>

<p>You maye have multiple issues here:
First and third options are a not a valid xpath. Second options may find more than one matches, so it will return the first that fits, which is not necessarily the one you want. So I suggest:</p>

<ol>
<li><p>Make <em>find</em> specific enough to locate a proper element. I'd suggest <a href="http://selenium-python.readthedocs.io/locating-elements.html#locating-hyperlinks-by-link-text" rel="nofollow">find_element_by_link_text</a> if you know the name of the link you are going to choose:</p>

<pre><code>link = driver.find_element_by_link_text('Stack Overflow')
</code></pre></li>
<li><p>Given you chose the right link, you should be able to get the attribute:</p>

<pre><code>href = link.get_attribute('href')
</code></pre></li>
<li><p>If the first statement throws an exception (most likely <em>element not found</em>), you may need to wait for the element to appear on the page, as described <a href="http://stackoverflow.com/questions/7781792/selenium-waitforelement">here</a></p></li>
</ol>

python selenium