How to use Selenium to click on a link - python

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"]]
...

Related

Using beatifulsoup to find text on html

This is my first time using beautifulsoup as a scraper tool and I just follow thru slowly with each step.
I've used soup.find_all("div", class_="product-box__inner") find a list of element I want and this partiful stuff not going thru my mind right now. my question below,
here is the HTML and my target is "$0" and I have tried
element.find("span", title= re.compile("$")) and I can't use element.select("dt > dd > span > span") because there's multiple one with same tag format which I dont need at all, Is there way I can target span data-fees-annual-value="" to get .text working?
<div class="product-box__features-item">
<dt class="f-body-3 product-box__features-label">Annual fee</dt>
<dd class="f-title-5 product-box__features-text u-margin-0">
<span>
<span data-fees-annual-value="">$0</span>
</span>
</dd>
</div>
You are close to your goal with css selectors and they could be used more specific and reference directly on the attribute data-fees-annual-value:
soup.select_one('span[data-fees-annual-value]').text
Example
from bs4 import BeautifulSoup
html="""
<div class="product-box__features-item">
<dt class="f-body-3 product-box__features-label">Annual fee</dt>
<dd class="f-title-5 product-box__features-text u-margin-0">
<span>
<span data-fees-annual-value="">$0</span>
</span>
</dd>
</div>
"""
soup=BeautifulSoup(html,"html.parser")
soup.select_one('span[data-fees-annual-value]').text
Output
$0
If you want to find element by text, use string instead of title:
element.find("span", string=re.compile('$'))
Output:
<span data-fees-annual-value="">$0</span>

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

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 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 to use the cssSelector :nth-child(n) to locate element in Python Selenium

I'm using Python Selenium to locate element by using nth-child(n).
Below is my html code:
<div id="iopop" style="">
<div style="" class="">
<div id="iopoph" class="animated zoomIn" style=" ">
<span style="" class="gs_hover"></span>
<b class="in">FALAFEL</b>
<a iid="128-73" class="itemsub lowend" price="2.99" name="FALAFEL (6)" style="">
<b class="in">(6)</b>
<b class="is"></b>
<b class="ip">2.99</b>
<b class="iq"></b></a>
<a iid="128-74" class="itemsub lowend" price="4.99" name="FALAFEL (12)" style="">
<b class="in">(12)</b>
<b class="is"></b>
<b class="ip">4.99</b>
<b class="iq"></b>
</a>
<b class="is"></b>
<b class="ip"></b>
<b class="iq"></b>
</div>
</div>
</div>
Now I want to locate the first a tag by using nth-child(n) so I tried:
driver.find_element_by_css_selector('div#iopoph a:nth-child(2)').click()
But there is an error says:
NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"div#iopoph a:nth-child(2)"}
(Session info: chrome=79.0.3945.88)
Any friend can help?
You were close but you need to consider a couple of things:
div#iopoph will identify the parent node, i.e.
<div id="iopop" style="">
So you need to traverse to it's grand child node:
<div id="iopoph" class="animated zoomIn" style=" ">
To locate it's child elements you can use the following Locator Strategies:
Locating <a iid="128-73" class="itemsub lowend" price="2.99" name="FALAFEL (6)" style="">:
div.animated.zoomIn#iopoph a:nth-of-type(1)
Locating <a iid="128-74" class="itemsub lowend" price="4.99" name="FALAFEL (12)" style="">:
div.animated.zoomIn#iopoph a:nth-of-type(2)
you can use this locator a:nth-of-type(2)
In your case example-
div#iopop a:nth-child(3) -> returns first a <anchor> tag i.e. <a iid="128-73"
div#iopop a:nth-child(4) -> returns second a <anchor> tag i.e. <a iid="128-74"
nth-child(index):
nth-child(index) method is used to select/get specified index child element. But the specified index element should be same as mentioned before colon ( : ). In your case-
div#iopoph a:nth-child(2)-> It points to tag and it is not same as the tag you mentioned before : (colon). so it returns NOSUCHELEMENTEXCEPTION

Categories

Resources