WebDriver not able to locate element by xpath using Java - java

I've been working at this for some time now. I'm using Selenium and WebDriver version 2.45 (with all browsers). I'm using Java, which should be arbitrary.
What I'm doing is simply find an element and hover over it, which I have done in earlier code. But for some reason, I can't get this one to work. I'm trying to get an element with this xpath, obtained by right-clicking the element in the HTML in Chrome and clicking "copy xpath":
.//*[#id='at-container']/div/div/div/form/div/div[1]/input[4]
What I have tried and did not work:
WebElement userName_editbox = driver.findElement(By.xpath("//div[#class='formelements' and #name='login']"));
WebElement userName_editbox = driver.findElement(By.xpath("//input[#id='at-container']/div/div/div/form/div/div[1]/input[4]]"));

Related

JAVA - Hidden Class Selenium

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..']")));

How to get to the element through JS in iframe? Selenium, Java

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);

Why am I not able to click the web element using the following code, even though I can see that the controller is tracking the xpath?

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);

Selenium cssSelector works in IDE but not in Webdriver

I have a web system I am automating using Java/Selenium Webdriver. I have an item I am trying to get access to. It has a compound class name. I have tried all the solutions I have been able to find here and so far none of them work.
The most offered solution looks like this:
By elem = By.cssSelector("div.prdbox.saleshdr");
List<WebElement> elements = driver.findElements(elem);
System.out.println("Number of Items found: "+elements.size());
When I check the size of the elements array it is always zero.
What I am finding however is that when I put the selector string in to the Selenium IDE (2.9.1) and use the "Find" button it identifies the correct web element without any problem at all.
I am at a loss for why it works in the IDE but not in my code.
Try selecting the element using its XPath? In the past when I ran into issues trying to select something using cssSelector, I often had success when I tried its XPath instead.
Give some wait time before the selector you are taking.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
List<WebElement> elements = driver.findElements(By.cssSelector("div.prdbox.saleshdr"));
System.out.println("Number of Items found: "+elements.size());
or try to find the elements with the help of Xpath or id.
List<WebElement> elements = driver.findElements(By.xpath("your xpath"));
Hope it will help you

Xpath selector not working in IE but working fine in Chrome and Firefox

I am using following xpath to click on element using JSExecutor in Selenium webdriver. This works fine in Firefox and chrome but does not work in IE.
Any idea to make this work? After lot of trial and error I have made this work in FF and chrome and have come up with the following XPath.
//*[contains(#class,'ui-select-choices-row') or contains(#id,'ui-select-choices-row')]//*[text()='TextofMyElementToBeclicked'
Additional info: This is a Jquery drop down on an angularJS application. When the user clicks on the drop down //ul is loaded and i am using the above xpath (which is part of //ul) to select the element based on text (using Javascript executor click). I used JS executor because, click() function in selenium simply could not click on the drop down element.
I am clicking element using below.
WebElement element = driver.findElement(By.xpath("YourNumbersXpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
enter code here
I successfully tested your XPath with IE11, so it's not an issue related to IE. It's most likely a timing issue. First click on the drop button, then wait for the targeted element to appear and finally click on it:
WebDriver driver = new FirefoxDriver();
WebDriverWait wait = new WebDriverWait(driver, 30);
driver.get("...");
// move the cursor to the menu Product
WebElement element = driver.findElement(By.xpath("drop down button")).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("drop down item"))).click();
IE11 seems to struggle with contains(#class and possibly also the contains(#id. Try using alternative solutions like starts-with.

Categories

Resources