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..']")));
Related
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'm coding test with Selenium Webdriver (Java), getting https://cloud.google.com as a driver.
I start with finding search input field, sendKeys("search phrase \n"). After that page starts changing its content and I'm trying to intersept these changes with WebDriverWait:
// first Wait - is to wait before page starts changing is content by removing search google icon
new WebDriverWait(driver, 30).until(ExpectedConditions.invisibilityOf(searchInputFieldIcon));
//second Wait - i'm waiting new hyperlink to appear (this hyperlink appears in search results after the whole page is asynchronically reloaded without page reloading)
new WebDriverWait(driver,30)
.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[#href='https://cloud.google.com/products/calculator']")));
The point is, that Wait doesn't waits for 30 seconds before element shows up. Code just throws an exception:
org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element: {"method":"xpath","selector":"//a[#href='https:`//cloud.google.com/products/calculator']"}`
Any help will be much appreciated!
Please check the attached screenshot. Here the Href link is different then you have used in your code.
You can use the below code
wait.until(expectedConditions.visibilityOfElementLocated(By.linkText("Google Cloud Platform Pricing ")));
To locate the first search result you can use the following xpath;
//a[contains(text(),'Google Cloud Platform Pricing')]
Checking your xpath
You can check whether your xpath is correct or not from the browser itself.
Go to DevTools (Ctril + Shift + I)
In the 'Elements' tab, press Ctrl + F
Input the xpath that you want to check
And it will show you whether it is correct and how many web-elements can be located from it.
In my java and Selenium project, I have the below method to click on a specific element
public void click(String xPath) {
driver.findElement(By.xpath(xPath)).click();
}
Currently, i have an element that it does not react on my click! So, i need to make sure, if selenium really successfully click on this element.
Point: There is no error during the runtime. So, i think it can find the element. But, it is a surprise for me, that there is no effect of click!
More details:
I am trying to automate this page. You can find the complete code in this repository. You need to take a few steps till you reach to the point that i really have the problem. That's why i share the repository, since the page is not directly accessible (sorry for that).
When i am in the https://hello.friday.de/quote/selectFuelType, (please find the image)
I am not able to click on the item (Benzin) with the below xpath:
final String fuel = "//*[#id=\"root\"]/div/div[3]/div/div[2]/div/div/form/div[2]/div[2]/button[1]";
During the runtime, there is no error, so, i expect that I can successfully click on the element, or successfully get the current url as /selectFuelType but none of them are working.
The problematic method is the i_am_asked_to_specify_the_Fuel_Type_of_the_car() in the RegisterInsuranceSteps class.
Your xpath can be much simpler and stabler. Try this:
final String fuel = "//button[contains(., 'Benzin')]"
See more details about contains method at MDN web docs or check this SO answer to see how to check against attributes or tag names.
Regarding verifying if button was really click, I think it's enough if your next test step fails. In your case it would be choosing the engine power.
On the website https://hello.friday.de/quote/selectPrecondition the first option Das Auto ist schon versichert is selected by default. So click() the second option Das Auto wird noch zugelassen oder umgemeldet you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class^='RadioButtonListField__container--'] button:nth-child(2)"))).click()
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(#class, 'RadioButtonListField__container--')]//following-sibling::button[1]"))).click();
Note: These are relative locators as per the relevant HTML and are not hard-coded to the exact text of the options.
Firstly if selenium doesnt click then try to use alternate xpath. You can also use css selectors.
Secondly if you want to check if click has happened and next page is loaded then you can simply check change in title or you can check if some element has loaded from next page.
click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);
I am using Java and Firefox and Firebug
I am not able to locate the element on the second page. For example if I login to gmail then I am not able to locate and click on the sent items or any other button on the next page.
I tried with the xpath (both absolute and relative) but every time I am getting an error that element not found.
with the code I am successfully able to login but as soon as the page loads I get an error "Element not Found".
Please suggest any solution
Unless you are telling WebDriver to wait until the element on the 2nd page is loaded, WebDriver will simply try to click the element as soon as its able to run. This is bad because your element might not yet be loaded while WebDriver is already trying to click it... TIMEOUT mayhem ensues...
Try the following... use the WebDriverWait class to make WebDriver wait for the element on the page to be loaded before attempting to click it...:
WebDriverWait wait = new WebDriverWait(driver, 100);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
element.click();
The '100' in WebDriverWait(driver, 100) is the maximum amount of seconds you want WebDriver to repeatedly attempt to locate the element before it times out...
I agree with the answer by CODEBLACK. Also you can go for Imlicit wait,which would make selenium wait implicitly for a given period of time.
Try following:-
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
You can specify time as per your convenience.
Best O Luck. . .!