Selenium can't seem to click on an href link - python

<a class="page-link" title="Next page" data-page-number="3" href="/recruiter/smartsearch?searchHistoryId=1702735186&searchCacheKey=1fdcde45-657b-4666-824a-12ab561b3a91%2Cm9d5&searchRequestId=d371cf7e-2703-43de-9fe5-218fad50e152%2C5wG7&searchSessionId=1702735186&linkContext=Controller%3AsmartSearch%2CAction%3Asearch%2CID%3A1702735186&doExplain=false&start=50" rel="next">
<svg aria-hidden="true">
use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#chevron-right-icon" data-size="small"></use></svg><span class="hide-a11y">Go to page 3</span></a>
I'm using a call to
driver.find_element_by_css_selector('a[title="Next page"]').click()
but this returns an empty string

Try using xpath
//a[#title='Next page']

Related

Finding a button by the span text in python selenium

<li id="b43d980c-53bb-4892-8f32-5ae8ebd9026e" class="">
<input type="button">
<span>Male</span>
</li>
How can I locate this and then click on it?
Text() attribute in xpath locator should do the trick to find the span:
driver.find_element(By.XPATH("//span[text()='Male']"))
Want to grab the input before?
driver.find_element(By.XPATH("//span[text()='Male']/preceding-sibling::input"))

How to get click on 'href' link using selenium

how do i click on the href link: href="/in/x-y-07976b159/" using selenium?
<a data-control-id="SOR2sXWgS8mDhCUEgFncpQ==" data-control-name="search_srp_result" href="/in/x-y-
07976b159/" id="ember1502" class="search-result__result-link ember-view"> <h3 id="ember1503"
class="actor-name-with-distance search-result__title single-line-truncate ember-view"> <span
class="name-and-icon"><span class="name-and-distance">
<span class="name actor-name">x y</span>
<span data-test-distance-badge="" id="ember1504" class="distance-badge separator ember-view">
<span class="visually-hidden">
1st degree connection
</span>
<span class="dist-value">1st</span>
</span>
</span><!----></span>
</h3>
</a>
You can click it with the help of below command.
driver.find_elements_by_xpath("//a[#id='ember1502']").click();
Where driver is the web driver instance.
You can do it both ways.
1) Find this element (eg. by id) and invoke method .click() on it.
2) Go to page (using driver.get(youcurrenturl + yourextracted href))
Another way of doing using find_element_by_idmethod.
driver = webdriver.Chrome()
driver.get('http://www.YourUrl.com')
driver.find_element_by_id('ember1502').click()

how do I select xpath image without a class name using selenium in python?

How do i select an image xpath without a classname. HTML code is like this
<img alt="" class src="https://images.craigslist.org/00J0J_i9BI6mN6rKP_300x300.jpg">
If I right click and copy xpath it gives me this //*[#id="sortable-results"]/ul/li[1]/a/img but when I use it in my code it has some error
In my code i use like this
src = driver.find_elements_by_xpath('/li[#class="result-row"]/a[#class="result-image gallery"]/img[#class=""]/#src')
but it returns me an [] when i print(src)
Full div
<li class="result-row" data-pid="7017735595">
<a href="https://vancouver.craigslist.org/van/ele/d/vancouver-sealed-brand-new-in-box/7017735595.html" class="result-image gallery" data-ids="1:00J0J_i9BI6mN6rKP"><img alt="" class="" src="https://images.craigslist.org/00J0J_i9BI6mN6rKP_300x300.jpg">
<span class="result-price">$35</span>
</a>
<p class="result-info">
<span class="icon icon-star" role="button" title="save this post in your favorites list">
<span class="screen-reader-text">favorite this post</span>
</span>
<time class="result-date" datetime="2019-11-11 00:52" title="Mon 11 Nov 12:52:25 AM">Nov 11</time>
Sealed - Brand New in Box - Google Home Mini
<span class="result-meta">
<span class="result-price">$35</span>
<span class="result-hood"> (Vancouver)</span>
<span class="result-tags">
<span class="pictag">pic</span>
</span>
<span class="banish icon icon-trash" role="button">
<span class="screen-reader-text">hide this posting</span>
</span>
<span class="unbanish icon icon-trash red" role="button" aria-hidden="true"></span>
<a href="#" class="restore-link">
<span class="restore-narrow-text">restore</span>
<span class="restore-wide-text">restore this posting</span>
</a>
</span>
</p>
</li>
The xpath is close. You need to use // at the beginning of the path and remove the /#src
//li[#class="result-row"]/a[#class="result-image gallery"]/img[#class=""]
If you want to make sure the element has src attribute it's like that
//li[#class="result-row"]/a[#class="result-image gallery"]/img[#class=""][#src]
To get the src attribute use get_attribute('src)
src = driver.find_elements_by_xpath('//li[#class="result-row"]/a[#class="result-image gallery"]/img[#class=""]')[0].get_attribute('src')
Note that find_elements return list, use index to get the first element.
If you want to use class="result-info" to locate the element you can do
elements = driver.find_elements_by_xpath('//p[#class="result-info"]/../a[#class="result-image gallery"]/img[#class=""]')
for element in elements:
src = element.get_attribute('src')
Actually the xpath has been copied correctly,
You have used it in a wrong way in the fetch code.
If you want the specific image, use
image = driver.find_element_by_xpath('//*[#id="sortable-results"]/ul/li[1]/a/img')
Or, if you want a list of all images of same xpath type, use:
images = driver.find_elements_by_xpath('//*[#id="sortable-results"]/ul/li/a/img')
(i.e. remove the specific number of 'li' div or any other div that you want to generalise and use find_elements; you need to use find_element for fetching a specific single element)
To get the attribute 'src', use get_attribute method:
For case 1:
website = image.get_attribute('src')
For case 2:
website = images[0].get_attribute('src')

How to use Selenium to click on a link

I want to click a link using Selenium with Python. The text is "104" in the following html code:
<a class="_2x4v" href="/ufi/reaction/profile/browser/?ft_ent_identifier=2411812215520941&av=200007162833925" rel="ignore" role="button">1045
<span aria-hidden="true" class="_1g5v">
<span data-hover="tooltip" data-tooltip-uri="/ufi/reaction/tooltip/?ft_ent_identifier=2411812215520941&av=200007162833925">104</span>
</span>
<span class="_4arz">
<span data-hover="tooltip" data-tooltip-uri="/ufi/reaction/tooltip/?ft_ent_identifier=2411812215520941&av=200007162833925">104</span>
</span>
</a>
I tried
driver.find_element_by_xpath('//a[text()="104"]').click()
But I received an error:
no such element: Unable to locate element: {"method":"xpath","selector":"//a[text()="104"]"}
What should I do?
Link has no child text node "104" - it's child text node of span. You can try below options to match required link:
//a[span="104"]
or
//a[.//text()="104"]
or
//a[.//span[text()="104"]]
...

selenium can not find html element on firefox

I try to verifiy in selenium RC with python:
sel.is_element_present("//div[#id='USER_PHOTOS']/div[1]/span[1]/img)
but looks like can not get it right.
and tried this:
sel.is_element_present("//div[#id='USER_PHOTOS'])
it works.
did anyone why it can't find the sub tag here:
<div class="bx01 deals swirllinks">
<div id="USER_PHOTOS" class="bx01">
<div class="ug_photos">
<span class="u_/LocationPhotos-g294212-d1234659-Fairmont_Beijing-Beijing.html#41195738 fkASDF fkLnk" onclick="setPID(3901); return ta.call('ta.overlays.Factory.photosLB', event, this);">
<img class="ug_photo" alt="coffee machine" src="http://ccm.ddcdn.com/ext/photo-l/02/5d/7c/f6/coffee-machine.jpg"/>
</span>
<span class="u_/LocationPhotos-g294212-d1234659-Fairmont_Beijing-Beijing.html#41195739 fkASDF fkLnk" onclick="setPID(3901); return ta.call('ta.overlays.Factory.photosLB', event, this);">
<img class="ug_photo" alt="bathroom" src="http://ccm.ddcdn.com/ext/photo-l/02/5d/7c/f7/bathroom.jpg"/>
</span>
In your xpath - //div[#id='USER_PHOTOS']/div[1]/span[1]/img , selenium will look for a div under div id = USER_PHOTOS. Your span doesn't come under "USER_PHOTOS" its under the <div class="ug_photos">
You should use //div[#class='ug_photos']//span[1]/img

Categories

Resources