Find element by inner element text using selenium - selenium-webdriver

I want to click an element by the text of a span who is the child of the element. How do I do this?
<button type="button" class="alert-button ion-focusable ion-activatable sc-ion-alert-md" tabindex="0"><span class="alert-button-inner sc-ion-alert-md">OK</span><ion-ripple-effect class="sc-ion-alert-md md hydrated" role="presentation"></ion-ripple-effect></button>
This is what I have tried but it didn't work.
#FindBy(css = "button[span:contains('OK')]")

Selenium doesn't supports the :contains pseudo-class anymore as #jari.bakken in their comment confirms:
AFAICT, the :contains pseudo-class isn't in the CSS spec and is not supported by either Firefox or Chrome (even outside WebDriver).
Further #dawagner in their comment also mentions:
contains isn't a valid CSS3 selector; because of this several browsers don't natively support contains, so Selenium (and WebDriver) don't claim to support it._
Solution
However you can still use the other attributes to construct a CssSelector to identify the element as follows:
#FindBy(css = "button.alert-button.ion-focusable.ion-activatable.sc-ion-alert-md span.alert-button-inner.sc-ion-alert-md")
As an alternative you can also use either of the following Xpath based locator strategies:
#FindBy(xpath = "//button[#class='alert-button ion-focusable ion-activatable sc-ion-alert-md']//span[#class='alert-button-inner sc-ion-alert-md']")
Using the innerText:
#FindBy(xpath = "//button[#class='alert-button ion-focusable ion-activatable sc-ion-alert-md']//span[#class='alert-button-inner sc-ion-alert-md' and text()='OK']")
or even:
#FindBy(xpath = "//span[#class='alert-button-inner sc-ion-alert-md' and text()='OK']")
even possibly:
#FindBy(xpath = "//span[text()='OK']")

CSS Selectors with Selenium do not support locating elements by their text content. Only XPath supports that.
Try this XPath:
//button//span[text()='OK']
or
//button//span[contains(.,'OK')]
or
//button//span[contains(text(),'OK')]
So the element could be defined as following
#FindBy(xpath= "//button//span[text()='OK']")
Or
#FindBy(xpath= "//button//span[contains(.,'OK')]")

Related

I'm having trouble locating a linkText with <em> tag in selenium with java

the HTML looks like
<span class="linktext"><em>M</em>asters</span>
xpath - //*[#id="mastersNavButton"]/span
I tried with below codes but didn't work
driver.findElement(By.xpath("//*[#id="mastersNavButton"]/span"));
driver.findElement(By.partialLinkText("asters")).click();
First check this:
List<WebElement> emElements = driver.findElements(By.tagName("em"));
System.out.println(emElements.size());
If you get 0, you'll need to wait until the page is loaded and elements reachable by selenium and/or switch to frame.
See this https://www.guru99.com/implicit-explicit-waits-selenium.html
and this https://www.guru99.com/handling-iframes-selenium.html
Please use the following code:
driver.find_element_by_xpath("//span[contains(#class,'linktext')]//em[contains(text(),'M')]").click()

Having issue with selecting drop down in selenium webriver

I have a sample HTML source of the dropdown.
I have tried with all possibilities but I having
"Exception in thread "main"
org.openqa.selenium.ElementNotInteractableException: element not
interactable" error in selenium web driver.
Plz, give me a solution to select the dropdown values in the web driver. What should I use?[HTML source here][1]
WebElement clickclientdrpdown=driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div[4]/div/form/div[1]/span/span[1]/span/span[1]"));
clickclientdrpdown.click();
WebElement selectclientdrpdown = driver.findElement(By.xpath("/html/body/div[5]/div[3]/div[1]/div/div[4]/div/form/div[1]/span/span[1]/span/span[1]"));
selectclientdrpdown.sendKeys("1 Private solution");
Your xpath is prone to breaking easily if the format of the HTML ever changes, just use findElement(By.Name), the name attribute is less likely to change as it is part of the Form and name is the parameter name passed to the server:
//Selenium method specific, prone to failure if element is disabled or not visible
WebElement selectclientdrpdown = driver.findElement(By.name("companyId"));
selectclientdrpdown.sendKeys("1 Private solution");
//Using the JavascriptExecutor
JavascriptExecutor js = (JavaScriptExecutor)driver;
js.ExecuteScript("document.querySelector("select[name='companyId'].value = '1 Private solution';");

Unable to locate SVG elements through xpath on Kendo UI chart

I did try some of xpaths but seems no luck.
I want to click on country and then graph , Given below screenshot :
Website URL is : https://demos.telerik.com/kendo-ui/bar-charts/column
I tried xpaths :
//text(text()='India')
//g//text(text()='India')
Hi you can click India with the following Xpath //*[text()='India']
This is a really helpful resource
I usually open chrome inspector and then hit cntrl+F to open up an interactive way to test my xpaths:
You can target the svgs by using their strokes, but note these may change often. example: //*[#d='M54.5 164.5 L 70.5 164.5 70.5 236.5 54.5 236.5Z' and #stroke='#03a9f4']
The elements on chart are from SVG-namespace, so you cannot use common syntax to select those elements (you wouldn't be able to select element by its tag name, e.g. //svg or //path, etc)
You can try below to select text node with text "India":
//*[name()="text" and text()="India"]
As the desired elements are SVG Elements you need to consider the namespace and induce WebDriverWait for the desired element to be clickable and to click on the first bar within the graph you can use the following solution:
Code Block:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://demos.telerik.com/kendo-ui/bar-charts/column")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='chart']//*[name()='svg']//*[name()='g']//*[text()='India']//following::*[name()='path']"))).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[#id='chart']//*[name()='svg']//*[name()='g'][contains(#clip-path, 'url')]//*[name()='path']"))).click()
Browser Snapshot:

Matching elements by property ends with in WebDriverIO

I am used to Selenium WebDriver were I can do something like this:
ReadOnlyCollection<IWebElement> magicPills = _webDriver.FindElements(By.CssSelector("span[id$='_Blue_Pills']"));
How do I do the same thing in WebDriverIO? I couldn't find anything in the docs that stated StartsWith, EndsWith, or whatever.
My first failed attempt is:
const magicPills = $('span.$_Blue_Pills');
Give a try as like below in wdio:
const magicPills = $$('span[id$='_Blue_Pills']');
$() returns a webElement not elements
and you can use the same cssSelector you tried in selenium_webdriver(because wdio will automatically resolves to cssSelector internally).
Please try the above and see if it works.

Purpose of findElement method in WebElement (Selenium)

I am learning Selenium WebDriver and I am facing some trouble.
I am unable to get the purpose of the methods findElement,findElements in WebElement when we already have them in WebDriver. What is the difference between the methods in WebElement and WebDriver?
WebDriver driver;
WebElement webObject;
driver.findElement() searches for the element/s on the entire web page while webObject.findElement() searches for the element/s within the webObject object.
Example:
webObject = driver.findElement('some webtable');
webObject.findElement('some cell') :: searches for the cell within that particular table.
driver.findElement('some cell') :: searches for the cell within the entire web page.
Say you have a
<div id="parent">
<a id="child">child</a>
</div>
You can do
WebElement div = driver.findElement(By.id("parent"));
WebElement a = div.findElement(By.id("child"));
so you're able to search inside the elements

Resources