I am writing a script to locate "login" and click on it for an web based application but I am getting exception:
no such element: Unable to locate element
My code:
System.setProperty("webdriver.chrome.driver","D:\\Selenium\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.wayn.com"); //to find login
driver.findElement(By.xpath("//*[#id='TopMenu']/div[1]/div/div[2]/login-buttons/div/div[1]/div[1]"))
.sendKeys(Keys.ENTER);
Try this way.
driver.get("http://www2.wayn.com");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[#class='button big left red ng-isolate-scope'][#wayn-log-click='loginButtonsLogClick']")).click();
Explanation of xpath: Use class and wayn-log-click attribute along with <div> tag.
Suggestion :- Instead of using absolute xpath, use relative xpath.
try waiting a little for element to be visible:
WebDriverWait wait =new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("username")));
driver.findElement(By.name("username")).sendKeys("username");
driver.findElement(By.name("password")).sendKeys("password");
Related
I am using Selenium Webdriver with testng. On one of the WebElements I am trying to perform click on button action.
I am picking the locator "ID" and performing the below action.
WebDriver webDriver = this.getWebDriver();
webDriver.findElement(By.id("addTagBtn")).click();
and i am getting below error -
javascript error: a.tagName.toUpperCase is not a function error
HTML code looks like below
This error message...
javascript error: a.tagName.toUpperCase is not a function error
...implies that the WebDriver instance attempted to invoke click() on the desired element even before the element was completely rendered with in the DOM Tree.
The desired element is a Angular element. So to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
id:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.id("addTagBtn"))).click();
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("td > button#addTagBtn"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//td/button[#id='addTagBtn' and text()='Add']"))).click();
It worked when i used Javascript code i.e
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("document.getElementById('addTagBtn').click();");
```
I have received this error for several times:
Unable to locate element: {"method":"xpath","selector":"//li[#id="tablist1-tab3"]"}
Code that I have used is:
options.addArguments("--headless");
options.addArguments("window-size=1200x900");
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
WebElement tab = driver.findElement(By.xpath("//li[#id=\"tablist1-tab3\"]"));
tab.click();
Can someone help me with this error?
You need to use WebDriverWait for the elementToBeClickable() and you can use either of the following Locator Strategies:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("li#tablist1-tab3"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//li[#id=\"tablist1-tab3\"]"))).click();
WebElement tab = driver.findElement(By.xpath('//li[#id="tablist1-tab3"]'));
try this
Make sure the element is not inside an iFrame. If it is, then you need to first find the iFrame element and use Selenium's switchTo(). After that you will be able to locate elements inside the iframe.
I have an issue to accept a cookie alert on the below website which is embedded in an iframe:
https://www.hamburg.de/
I've tried many ways to solve the issue with using the driver.switchTo().frame() - method:
by using the Id of the iframe sp_message_iframe_234327
by inserting an index for the iframe 0
by calling the iframe through a WebElement WebElement element = driver.findElement(By.xpath("//body/div[6]/iframe[#src='https://cdn.privacy-mgmt.com/index.html?message_id=234327&consentUUID=b2cb90ea-dfdd-4655-b6b9-89117ff34893&requestUUID=ccb96546-c6b5-44e7-9869-438b32f7ad89&preload_message=true']")); driver.switchTo().frame(element);
Unfortunately none of them are working. I'm always getting the following exception:
org.openqa.selenium.NoSuchFrameException
Does anyone have an idea on this specific example to switch on the iframe properly?
I'm working with Selenium WebDriver 3.141.59 on Java and my tests should be executed on Mozilla Firefox (80.0.1) and Chromium (85.0.4183.102). Those two browsers are launched headlessly.
Glad for any help.
To click on Alle akzeptieren within the url https://www.hamburg.de/, as the the desired element is within a <iframe> so you have to:
Induce WebDriverWait for the desired frameToBeAvailableAndSwitchToIt.
Induce WebDriverWait for the desired elementToBeClickable.
You can use either of the following Locator Strategies:
Using cssSelector:
driver.get("https://www.hamburg.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("iframe[id^='sp_message_iframe']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[aria-label='Alle akzeptieren']"))).click();
Using xpath:
driver.get("https://www.hamburg.de/");
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#id, 'sp_message_iframe')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#aria-label='Alle akzeptieren']"))).click();
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Is it possible to switch to an element in a frame without using driver.switchTo().frame(“frameName”) in Selenium Webdriver Java?
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 a checkbox but I'm not able to and didn't find any solution in other threads.
Here is my HTML code for drop down list:
code:
#FindBy(css="input.ng-valid.ng-dirty.ng-touched")
WebElement chrgAllocFee;
chrgAllocFee.click();
but it is not working where as other checkboxes are working when I define and use in same way. I found that the difference as compared to others is that here html code has additional line with
Please suggest how to define so that it can be recognised.
Error getting:
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"input.ng-valid.ng-dirty.ng-touched"}
(Session info: chrome=69.0.3497.92)
The desired element is an Angular element so you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("input.ng-valid.ng-dirty.ng-touched[name='ChargeAllocationFee']"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#class='ng-valid ng-dirty ng-touched' and #name='ChargeAllocationFee']"))).click();
I found solution:
I used javascript for clicking that field and it works fine and here is the code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", CamWizOR.chrgAllocFee);
CamWizOR.chrgAllocFee -> WebElement name
#FindBy(xpath="//input[#name='ChargeAllocationFee' and #type='checkbox']")
WebElement chrgAllocFee;