I am working on selenium, while running Java code I tried to CLICK a menu from the web page but encounter error of selenium.ElementNotVisibleException: Element is not currently visible Kindly advise on this matters . Thanks you
HTML code for text field :
<li onclick="goin('pages/AbcProxy/proxyGroupList.do')>
TESTABC
JAVA code:
WebdriverWait wait = new WebDriverWait(driver,50);
wait until(ExpectedConditions.presenceOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
presenceOfElementLocated checks if the element exists in the DOM. To check if the element is visible use visibilityOfElementLocated
WebdriverWait wait = new WebDriverWait(driver,50);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div/div/ul/li[12]/a")));
element.click();
It might be that your xpath is to a different a element that is invisible (css display none or such).
It would be better to use some id first (maybe for the parent ul?) and then a relative xpath from there, full xpaths from root are not recommended as it makes the test very brittle
Related
I have tried a different method to input elements like
Here is xpath I used
By NameOfTheProperty=By.xpath("//fieldset/div/input[#name='name']");
By NameOfTheProperty=By.xpath("//div/input[#name='name']");
By NameOfTheProperty=By.xpath("//input[#name='name']");
Tried with Selenium Builder
WebElement element=driver.findElement(by);
Actions builder = new Actions(driver);
Action mouseOverHome = builder
.moveToElement(element)
.click().sendKeys(text).build();
mouseOverHome.perform();
Tried with
WebElement element=driver.findElement(by);
element.sendKeys(text);
None of the methods is working..I can not able to input text inside the field and It shows
Element not interactable
Here is the site
I hope someone help me to find out the solution..
Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.
Steps to check:
Press F12 in Chrome -> go to element section -> do a CTRL + F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.
xpath you should checks is :
//input[#name='name']
if it is unique, then you can use Javascript executor :
WebElement password_input = driver.findElemenet(By.xpath("//input[#name='name']"));
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', 'password_should_be_written_here')", password_input)
personally I would use id's if the element has one.
"//*[#id='__BVID__104']"
Make sure you are waiting for the element to be ready.
WebDriverWait wait = new WebDriverWait();
wait.until(ExpectedConditions.elementToBeClickable(element));
If it then times out on wait.until(... then I've found that sometimes an element must first be clicked for it to be exposed.
If this is the case. You will have to inspect the element before it is clicked. Find it's xpath, and see if it changes when it is clicked. If so then create a webElement for that element and have Selenium first click it, before clicking the actual field/element.
I'm working on a program that uses Selenium and I'm trying to scroll on some aside div of the document.
I firstly tried the following line of code but this only scrolls the main part of the HTML document:
((JavascriptExecutor) driver).executeScript("scrollBy(0, 500)");
So I tried to execute this:
((JavascriptExecutor) driver)
.executeScript("document.getElementsByClassName('aside-div')[0].scroll(0,100)");
This compile and executes without any error but the section I want to scroll through doesn't scrolls. How can I do this?
The best practice is to always use the javascript code on your browser console before actual implementation.
Also, plz make sure scroll(x,y), the value should be according to the scroll it can be either (1000,0) or (0,1000) negative-positive according to the requirement. Kindly choose accordingly.
Ok so coming back to your actual problem, what I feel is you are incorrectly using the code
Either you have been using scroll(0,1000) instead of scroll(1000,0) so on..
You have not mentioned the exact element locator.
PS: Please provide the HTML for in-depth debugging.
Meanwhile, you can checkout the below code:
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(0,1000)");
//OR
((JavascriptExecutor) driver).executeScript("document.getElementsByClassName('aside-div'[0].scroll(1000,0)");
//OR
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div"));
((JavascriptExecutor) driver).executeScript("arguments[0].scroll(1000,0);", elementToScroll);
Please provide the exact element locator to use below code:
Example:
WebElement elementToScroll = driver.findElement(By.cssSelector(".aside-div table tr td"));
//This will scroll the page Horizontally till the element is found
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", elementToScroll);
I am a beginner in selenium and I would like to press a file submission field.
I have already done a whole code to connect to the page, click on the buttons etc. (everything works, my driver is good)
But impossible to click on adding file
I looked on the internet how to do it, I added time, tried to browse the frames, used javascript for the hidden class... I tried all the buttons in the field and it doesn't detect them.
Add File
Source code
Thread.sleep(2000);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver, 60);// 1 minute
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*#id=\"yui_3_17_2_1_1584634673387_348\"]/div[1]/div[1]/a")));`
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id="yui_3_17_2_1_1584634673387_348"]/div[1]/div[1]/a"}
Do you have an idea ?
The primary issue I observed looking at the code is the incorrect locator in your Explicit Condition element checking line. It can be replaced with below code:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#id='yui_3_17_2_1_1584634673387_348']/div[1]/div[1]/a")));
Basic XPath Syntax for reference though it has it's variations:
//tagname[#attrbute='value']
Additional advice Though I am not sure about the application you are automating, but the ID is likely to be changed. Based on the DOM Structure you have provided in the link above I would say change the locator to something on the lines of:
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#role='button'][#title='Add..']")));
I am using Java and Selenium Webdriver in order to test the functionalities of a single page web application.
For this reason, clearly, elements are injected and removed from the DOM dynamically.
I know I can wait for an element to be present in the DOM using similar code that is using WebDriverWait (very neat template I wrote slightly changing GitHub):
public void waitForElement() throws Exception {
/*
Inject the following snippet in any web page to test the method
<button class="i-am-your-class" onclick="alert('Wow, you pressed the button!');">Press me</button>
*/
System.setProperty("webdriver.gecko.driver", "C:\\Program Files (x86)\\Mozilla Firefox\\geckodriver.exe");
WebDriver driver = getDriver();
WebDriverWait wait = new WebDriverWait(driver, 10); // 10 can be reduced as per Test Specifications
driver.get("http://www.google.com");
WebElement response = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[#class='i-am-your-class']")));
response.click();
System.out.println("*****************************************************************************");
System.out.println(response);
System.out.println(response.getText());
driver.close();
}
What I would like to know is if this is also the more efficient way to obtain such result using an xpath.
I have been researching on Stackoverflow and several answers point in a similar direction but no answer is focused on efficiency / performances:
WebDriver - wait for element using Java
Selenium WebDriver: wait for element to be present when locating with WebDriver.findElement is impossible
Thanks for your time and help.
There are a couple of facts which you need to consider as follows :
WebDriverWait() for 100 shouldn't be a real-time waiter. Consider reducing it as per the Test Specifications. As an example, set it as 10 seconds :
WebDriverWait wait = new WebDriverWait(driver, 10);
Moving forward as you are invoking click() method so instead of ExpectedConditions as visibilityOfElementLocated() method use elementToBeClickable() method as follows :
WebElement response = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#class='i-am-your-class']")));
Optimize your xpath including the tagName as in By.xpath("//tagName[#class='i-am-your-class']"). As an example :
By.xpath("//a[#class='i-am-your-class']")
Optimize your code to invoke click() as soon as the element is returned through WebDriverWait as follows :
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#class='i-am-your-class']"))).click();
I do not know for what reason you have said XPATH is preferred. I would like to add something about locators.
The XPATH you have written in your code can be easily replaced by a css selector. Here is the example.
div[class='i-am-your-class']
Now the main question is why do you want to switch to different locators ?
more efficiency.
more fast as compared to xpath.
more reliable.
more performance.
You should always use locators in this order as suggested by Selenium contributors
ID
name
className
linkText
partialLinkText
tagName
cssSelector
XPath.
Note : Most of the time a cssSelector can replace Xpath, However Xpath has its own advantages which cssSelector do not provide.
For more reference you can go through this SO Link : Xpath vs Css selector
So what we have - there's an iFrame on page which shows a preview of previously generated data. I need to get element with text and then get CSS value from it.
Take a look at the DOM here:
example of the page
xpath to this element looks right to needed element (God save Chrome DevTools!).
But script cannot detect this element.
What I've did:
1 - switched to Iframe where my element is located - successfully.(no more iframe inside this iframe as per screenshot).
2 - tried to find element - NoSuchElementException.
Probably, such issue appears because of this #document thing ?If so - how do I can solve it and get to needed element with script ?
may be you need to wait till the frame to be loaded as given below.
WebDriverWait wait=new WebDriverWait(driver, 90);
driver=wait.untill(ExpectedConditions.frameToBeAvailableAndSwitchToIt("previewFrame");
WebElement element=driver.findElement(By.TagName("em"));
String fontStyle=element.getCssValue("font-sytle");
System.out.println(fontStyle);