Trying to click on a hyperlink that includes spaces - java

I am trying to click on a hyperlink with a space using Java and Selenium. Here is a sample of the code
<h3 class="side menu">
<a class="side-menu" href="/configurations">
<span class="menu-icon ca"></span>Configuration
</a>
</h3>
I have tried using xpath starts-with and contains with no luck.
driver.findElement(By.xpath("//a[starts-with(text(),’Configuration’)]")).click();

To handle dynamic element use WebDriverWait and elementToBeClickable with expected conditions.Use the following Xpath locator strategies.
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][normalize-space(.)="Configuration"]')));
element.click()
OR
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath('//a[#class="side-menu"][contains(.,"Configuration")]')));
element.click()

I think all you need is to add a forward slash:
driver.findElement(By.xpath("//a[starts-with(text(),'/configuration')]")).click();
It works for me in the Chrome console if I type:
$x("//a[starts-with(#href,'/configuration')]")

I would use normalize-space when writing the xpath for the text, which will trim the leading and preceding white chars (spaces/tabs/newlines)
Here is how you should do it in your question.
//a[normalize-space(.)='Configuration']
Tested the xpath in chrome console for click() successfully using javascript.

Related

Clicking a button found with xpath working in Chrome but not in IE11

I want to click a button(to send a form)
<button class="form-button primary">Click here</button>
I find the element like this:
driver.findElement(By.xpath("//button[contains(text(),'Click here')]")).click;
On chorme is working and sending the form but in IE11 is not(sending the form).
To be clear, in IE is finding the element(or an element). But probably is not the correct element.
Addition info:
This is the only button with this text
I can probably find other ways to get this element , but if I rework this path I will need to change all the paths similar to this.
Selenium version:3.14
IE webdriver :3.14
There are several different ways of clicking on something using selenium. I would try using either a javascript click or an action click.
Javascript click:
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Action click:
Actions action = new Actions(driver);
WebElement element = driver.findElement(By.xpath("//button[contains(text(),'Click here')]"));
action.moveToElement(element).click().build().perform();
It's also possible that you are in the wrong frame while you are executing the click.
driver.switchTo.frame("Frame_ID");
You would be able to find the frame ID when you inspect the webpage.
If your usecase is to invoke click() you have to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
xpath using className and text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='form-button primary' and text()='Click here']"))).click();
xpath using text:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Click here']"))).click();
xpath using contains():
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(., 'Click here')]"))).click();

How to click on Log In button/link data-testid using Selenium and Java

I am attempting to click on 'Log in' but my Selenium code isn't working. Below is the HTML code.
<span class="css-14krylx-text-text-fullPageText-FormFooter">Already have a Times account?
<span tabindex="0" data-testid="switch-to-login" class="css-dip6gw-link-link-FormFooter">Log in</span></span>
What I attempted so far and did not work?
driver.findElement(By.cssSelector("[data-testid='switch-to-login'")).click();
driver.findElement(By.xpath("//span[#data-testid='switch-to-login'")).click();
driver.findElement(By.cssSelector("/.css-dip6gw-link-link-FormFooter'")).click();
Is there any other approach?
The desired element is a dynamic element so to locate the element you have to induce WebDriverWait for the element to be clickable and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span[class$='text-text-fullPageText-FormFooter'] span[class$='link-link-FormFooter'][data-testid='switch-to-login']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[contains(#class, 'text-text-fullPageText-FormFooter')]//span[contains(#class, 'link-link-FormFooter') and text()='Log in']"))).click();

How to click a hyperlink without any link text

I am trying to click on a hyperlink without a link text.
I have:
<a id="fontColor" href="f?p=420181023:1:12264109389989:1416222:NO::P1_SEMCODE:20190">
<strong>Check</strong>
</a>
I've tried:
driver.findElement(By.xpath("//a[#href='f?p=420181023:1:12264109389989:1416222:NO::P1_SEMCODE:20190']")).click();
Causes No.SuchElementException
driver.findElement(By.id("fontColor")).click();
Does nothing
I have read different materials from different websites but it seems none mention hyperlinks without link text. Is there a alternative to
By.xpath()
By.id()
By.linkText()
Sources:
How to click a href link using Selenium
https://www.w3schools.com/html/html_links.asp
The desired element looks to be a dynamic element so invoke click() you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a#fontColor[href*='P1_SEMCODE']>strong"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#id='fontColor' and contains(#href, 'P1_SEMCODE')]/strong[contains(., 'Check')]"))).click();

Unable to locate the element as per the below anchor tag

Please advise how to locate the link for New business tag in the below code. I have tried the following xpath but it didnt work:
driver.findElement(By.linkText("NEW BUSINESS")).click
driver.findElement(By.xpath("//span[#class='hdBottomBar']/a[1]"))
HTML:
<span class="hdBottomBar">
<a class="hdTopBar" href="javascript: void navCntl('NewBusiness','NavBar');" onmouseover="window.status='New Business';return true" onmouseout="window.status='';return true" name="newBusiness">NEW BUSINESS</a>
The element is a JavaScript enabled element so to invoke click() you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
linkText:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("NEW BUSINESS"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span.hdBottomBar>a.hdTopBar[name='newBusiness']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[#class='hdBottomBar']/a[#class='hdTopBar' and #name='newBusiness'][text()='NEW BUSINESS']"))).click();
try with:
//span[#class='hdBottomBar']/a[#name='newBusiness']
or
//span[#class='hdBottomBar']/a[text()='NEW BUSINESS']

Selenium WebDriver -- get <span> text

I need to get the price value $726.35 from:
<span id="paiement-resultats"class="calculateur-resultats-total" style="" xpath="1">$726.35</span>
but it doesn't work:
driver.findElement(By.id("paiement-resultats")).getText()
How can I get this value?
Screenshot from the browser:
As the element is within a <span> tag, to extract the text $726.35 you can use either of the following solution:
cssSelector:
driver.findElement(By.cssSelector("span.calculateur-resultats-total#paiement-resultats")).getText();
xpath:
driver.findElement(By.xpath("//span[#class='calculateur-resultats-total' and #id='paiement-resultats']")).getText();
Update
As the results were unstable you can induce WebDriverWait for the visibilityOfElementLocated and you can use either of the following solutions:
cssSelector:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("table[aria-label='User']"))).getAttribute("innerHTML");
xpath:
String myText = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//table[contains(#style,'vertbar')]"))).getAttribute("innerHTML");
Try this one,
Below i have Mentioned Two Xpath Try With that, if not working Let Me Know
//div[#id='resultats']//following::span[#id='paiement-resultats']
//div[#id='resultats']//following::span[#class='calculateur-resultats-total']
driver.findElement(By.xpath("//h2[#class='resultats']//following::span[#id='paiement-resultats']")).getText()
Cheers!
I found the solution
driver.findElement(By.xpath("//span[#class='calculateur-resultats-total' and contains(text(),'$')]")).getText();

Categories

Resources