How to get click on 'href' link using selenium - python

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

Related

How i can click on a div with role button without text? Using Python Selenium

This is the html code exemple:
<div aria-label="Continue" class="my-class" data-visualcompletion="ignore"></div>
<div class="div1-class">
<div class="div1-class2">
<span class="area-span" dir="auto">
<span class="text-span">Continue</span>
</span>
</div>
</div>
<div class="div2-class" data-visualcompletion="ignore"></div>
I'm trying:
continue = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.CLASS_NAME, 'my-class')) )
continue.click()
but it doesn't work in any of the ways I tried.
You should check the xpaths in the browser console.
And try doing this ?
driver.findElement(By.xpath("//div[contains(#class,'my-class')]"));

Web element not found but appears when inspected manually

I am unable to access this menu in selenium the web element doesn't appear in inspector until manually done
<a id="cke_3275" class="cke_menubutton cke_menubutton__table cke_menubutton_off cke_menubutton__table" href="javascript:void('Table Properties')" title="Table Properties" tabindex="-1" _cke_focus="1" hidefocus="true" role="menuitem" aria-haspopup="false" aria-disabled="false" onmouseover="CKEDITOR.tools.callFunction(666,5);" onmouseout="CKEDITOR.tools.callFunction(667,5);" onclick="CKEDITOR.tools.callFunction(668,5); return false;">
<span class="cke_menubutton_inner">
<span class="cke_menubutton_icon">
<span class="cke_button_icon cke_button__table_icon" style="background-image:url(https://lms.testbook.com/vendor/ckeditor/plugins/icons.png?t=E6FD);background-position:0 -1896px;background-size:auto;">
</span>
</span>
<span class="cke_menubutton_label">
Table Properties
</span>
</span>
</a>
I tried accessing parent, click and actions.perform() nothing seems to work.
When i hover over the menu contents i see javascript:void('contentname'), i pasted this in the inspector and found the web element.
iframe=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "/html/body/iframe")))
driver.switch_to.frame(iframe)
driver.find_element_by_tagname("a")
switch to iframe and then add rest of the code
driver.switch_to.default_content()
you need to switch to default content after you are done with the element and want to interact with an element outside the iframe

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't seem to click on an href link

<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']

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