Finding a button by the span text in python selenium - python

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

Related

Python Selenium How to find elements by XPATH with info from TAG and SUB TAG

HTML:
<div id="related">
<a class="123" href="url">
<h3 class="456">
<span id="id00" aria-label="TEXT HERE">
</span>
</h3>
</a>
<a class="123" href="url">
<h3 class="456">
<span id="id00" aria-label="NOT HERE">
</span>
</h3>
</a>
</div>
I'm trying to find & click on <a (inside the div id="related" with class="123" AND where SPAN aria-label contains "TEXT"
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123'][contains(#href, 'url')]//span[contains(#aria-label, 'TEXT']")
But it's not finding the href, it's only finding the span.
then I want to do:
items[3].click()
How can I do that.
Your XPath has some typo problems.
Try this:
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123'][contains(#href,'watch?v=')]//span[contains(#aria-label,'TEXT')]")
This will give you the span element inside the presented block.
To locate the a element you should use another XPath.
UPD
To find all the a elements inside div with #id='related' and containing span with specific aria-label attribute can be clearly translated to XPath like this:
items = driver.find_elements(By.XPATH, "//div[#id='related']//a[#class='123' and .//span[contains(#aria-label,'TEXT')]]")

Selenium Can't find "HOME" element on Facebook

I'm doing some exercises with selenium, trying to scrape a few pages on facebook.
However, I'm not able to find the "Home" button link element to keep going. This on the left menu in any user's profile.
From what I'm seeing in the page code, the link is here:
<div class="_2yaa" data-key="tab_home">
<a class="_2yau" data-endpoint="/seudogshow/?ref=page_internal" href="/seudogshow/?ref=page_internal">
<span class="_2yav">Home</span>
<span class="img _55ym _55yn _55yo _2wwb" aria-busy="true" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuetext="Loading..."></span>
</a>
</div>
How would you guys go about clicking this button?
I tried with something like this:
driver.find_element_by_xpath("//a[contains(text(), 'Home')]").click()
or
wait.until(EC.element_to_be_clickable((By.XPATH, "//a[contains(text(), 'Home')]"))).click()
But I'm clearly doing it wrong.
The element you are trying to click has a span tag, not a. Try the following:
driver.find_element_by_xpath("//span[contains(text(), 'Home')]").click()

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

Finding elements in Selenium

My button looks like this:
<button type="button" data-toggle="modal" data-target="#modal-order-export" data-backdrop="false" class="btn btn-default btn-sm">
<i class="fa fa-file-excel-o"></i> 엑셀 저장
</button>`
How can I click this object via selenium to automate some work? I tried:
driver.find_element_by_link_text('엑셀 저장').click()
You could take its xpath by right-clicking the element in developer tools and choosing copy. Then just find the element using xpath.
The WebElement which you are trying to search/find is within <button> tag hence find_element_by_link_text may not work. We will be using an xpath to locate the WebElement as follows:
driver.find_element_by_xpath("//button[#class='btn btn-default btn-sm']/i[#class='fa fa-file-excel-o']").click()

Categories

Resources