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();
Related
I am trying to click a select element which is followed by ::before tag. Can anybody please help me to write the XPath or the CSSselector path for the below HTML
Below is the XPath that I have written to click on the month
driver.findElement(By.xpath("//div[contains(#class,'pika-single is-bound left-aligned bottom-aligned')]/div[contains(#class,'pika-lendar')]/div[contains(#class,'pika-title']/div[1]/select[contains(#class,'pika-select pika-select-month')]"));
Pseudo Elements
A CSS pseudo-element is used to style specified parts of an element. It can be used to:
Style the first letter, or line, of an element
Insert content before, or after, the content of an element
::before
::before is a pseudo element which allows you to insert content onto a page from CSS (without it needing to be in the HTML). While the end result is not actually in the DOM, it appears on the page as if it is.
You can find a couple of relevant detailed discussion in:
How locate the pseudo-element ::before using Selenium Python
Not able to get element by xpath inside div with ::before
This usecase
As the date picker is a html-select menu you have to use the Select Class. Moreover, as you have to interact with the drop-down-menu you have to induce WebDriverWait for the elementToBeClickable().
To select the <option> with text as January from the dropdown you can use you can use either of the following Locator Strategies:
Using cssSelector:
WebElement elem = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("select.pika-select.pika-select-month")));
Select mySelect = new Select(elem);
//selecting the item January by value
mySelect.selectByValue("0");
Using xpath and in a single line:
//selecting the item January by visible text
new Select(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//select[#class='pika-select pika-select-month']")))).selectByVisibleText("January");
You can find a couple of relevant detailed discussions in:
How to select an option from a dropdown through Selenium WebDriver
How can I get all elements from drop down list in Selenium WebDriver?
I have an issue with located correct Xpath/ID input field, I am taking the following website as an example:
https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine
After you open the link then scroll down a bit, you will see "write a comment" blue button, click it then fill the text and name, it will appear a reCaptcha example, I tried the following way to click the checkbox, but without success. I would really appreciate if someone can let me a hand on it
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(
"//iframe[starts-with(#name, 'a-') and starts-with(#src, 'https://www.google.com/recaptcha')]")));
new WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
Seems you were close enough. The name of the <iframe> i.e. a-x2cp4vfbaqu8 looks dynamic, so it would be better to avoid the name attribute and you can use either of the following Locator Strategies:
Using css_selector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/recaptcha/api2/anchor')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[#id='recaptcha-anchor']"))).click();
References
You can find a couple of relevant discussions in:
How to click on elements within an iframe to enable the captcha through the audio using Selenium and Python
Find the reCAPTCHA element and click on it — Python + Selenium
Dealing with reCAPTCHA in Python Selenium
I struggled with this for several days, it's actually very simple. The captcha is located in a frame, and in order to click on it, you need to switch to this frame.
I did it like this with Selenium:
driver.switchTo().frame(driver.findElement(By.xpath("//*[#title='reCAPTCHA']")));
driver.findElement(By.xpath("//span[#id='recaptcha-anchor']")).click();
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();
I'm trying to select the highlighted element below (which is a "window close" button):
There is one other element with class='icon-Dismiss' on the page, but none with class='dialog-close'.
What I have tried so far:
driver.findElement(By.xpath("//*[#class='icon-Dismiss' and #class='dialog-close']"))
driver.findElement(By.className("dialog-close"))
driver.findElement(By.xpath("//*[#id='contentBox']"))
In all cases however, I receive the following error:
no such element: Unable to locate element
Does anyone have an idea on how I can select this element?
You need to induce WebDriverWait for the desired elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.contentBox div.icon-Dismiss.dialog-close"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#id='contentBox']//div[#class='icon-Dismiss dialog-close']"))).click();
Try using
driver.findElement(By.cssSelector(".icon-Dismiss.dialog-close"))
Also, make sure this element is not in an iFrame. If it is then you will need to switch to the iFrame first before you can find the element.
try this driver.switchTo().alert().dismiss(); if that dialog box is an alert.
If you just want to locate then
driver.findElement(By.cssSelector("div.icon-Dismiss.dialog-close"));
but if you want to just close the alert then alert().dismiss(); will be the best option.
If this is the window pop up
try : driver.switchTo().alert().dismiss();
if this is application these locators should work
Note : make sure you are using waiting till popup appearing on page
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.