presenceOfElementLocated in selenium unable to locate presence of Webelemnt - selenium-webdriver

WebDriver driver =new FirefoxDriver();
driver.get("http://www.goibibo.com/");
WebDriverWait driverwait=new WebDriverWait(driver,60);
WebElement mydynamicElement=driverwait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='hdr_user_signin']/span/a[2]")));
Boolean number=mydynamicElement.isDisplayed();
System.out.println(number);
Answer i got is 'false' even though i put up a wait of 60 sec.
Don't know why unable to locate presence of Element....

mydynamicElement has probably been located successfully, but it is hidden. You are not asking Selenium to find ONLY if the element displayed with presenceOfElementLocated.
Meaning presenceOfElementLocated and visibilityOfElementLocated are not same. I believe you are looking for visibilityOfElementLocated. See the API doc here

Related

Selenium Web-Driver Popup

I am unable to write on this popup message using selenium.
Please feel free to help me in this case.
My Code is:-
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\gecko_driver\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.heycare.com");
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("hello world-----1");
driver.switchTo().frame(0);
driver.findElement(By.id("mobile")).sendKeys("7015273543");
System.out.println("hello world-----2");
//Driver.findElement(By.id("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
}
Error:-
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: *[name='mobile']
Code is working fine till:-
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
Unable to write on that popup form that comes after clicking on the login button..
I navigated to that website and successfully targetted the mobile number field using this css selector #mobile which targets the id attribute. You're doing this for your password field. Try having your test use this:
Driver.findElement(By.id("mobile")).sendKeys("7015273543");
See if that helps...
Edit: I'm not sure that the unformatted code block you're showing that switches frames is necessary. Unless you're working with iframes, I didn't see any in the minute I was looking around.
Edit2:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.heycare.com");
System.out.println("hello world");
driver.findElement(By.className("log-pop")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.name("mobile")));
driver.findElement(By.name("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
You could try something like this, I added in a call to WebDriverWait to account for the possible delay between clicking the login button, and waiting for the login prompt popup to finish it's animation prior to be allowed to be clicked. I'm not sure if this will work in your case, but it's a possibility I sniffed out when I was observing the site's behavior.
(I didn't test any of this code, it's freehand written. So take it with a grain of salt. It might not work flawlessly as is.
Bit of java nit, you should make your object references lowercase, save capitalized symbols for class names. I had a moment of trying to figure out why you were calling static methods of a class called Driver instead of an instance of the class WebDriver

Selenium, sendKeys to an element that´s not visible but selected [duplicate]

Here I have the image of my code and the image of my error. Can anyone help me to resolve this issue?
ElementNotInteractableException
ElementNotInteractableException is the W3C exception which is thrown to indicate that although an element is present on the HTML DOM, it is not in a state that can be interacted with.
Reasons & Solutions :
The reason for ElementNotInteractableException to occur can be numerous.
Temporary Overlay of other WebElement over the WebElement of our interest :
In this case, the direct solution would have been to induce ExplicitWait i.e. WebDriverWait in combination with ExpectedCondition as invisibilityOfElementLocated as folllows:
WebDriverWait wait2 = new WebDriverWait(driver, 10);
wait2.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("xpath_of_element_to_be_invisible")));
driver.findElement(By.xpath("xpath_element_to_be_clicked")).click();
A better solution will be to get a bit more granular and instead of using ExpectedCondition as invisibilityOfElementLocated we can use ExpectedCondition as elementToBeClickable as follows:
WebDriverWait wait1 = new WebDriverWait(driver, 10);
WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath_of_element_to_be_clicked")));
element1.click();
Permanent Overlay of other WebElement over the WebElement of our interest :
If the overlay is a permanent one in this case we have to cast the WebDriver instance as JavascriptExecutor and perform the click operation as follows:
WebElement ele = driver.findElement(By.xpath("element_xpath"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", ele);
I got this because the element I wanted to interact with was covered by another element. In my case it was an opaque overlay to make everything r/o.
When trying to click an element UNDER another element we usualy get "... other Element would receive the click " but not always :.(
This Exception we get when the element is not in an interactable state. So we can use wait till the element is Located or become clickable.
Try using the Implicit wait:
driver.manage().timeouts().implicitlyWait(Time, TimeUnit.SECONDS);
If this is not working use Explicit wait:
WebDriverWait wait=new WebDriverWait(driver, 20);
WebElement input_userName;
input_userName = wait.until(ExpectedConditions.elementToBeClickable(By.tagName("input")));
input_userName.sendkeys("suryap");
You can use ExpectedCondition.visibilityOfElementLocated() as well.
You can increase the time, for example,
WebDriverWait wait=new WebDriverWait(driver, 90);
A solution to this for Javascript looks like this. You will have to modify the time to suit your need.
driver.manage().setTimeouts({ implicit: 30000 });
Hope this is helpful to someone.
see the docs for reference
Actually the Exception is Element Not Visible
The best practice is to use Implicit wait below driver Instantiation so it get sufficient time to find element throughout the exception
driver.get("http://www.testsite.com");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Still facing issue as some element require more time. Use ExplicitWait for individual element to satisfy certain condition
In your case you are facing element not visible exception then use wait condition in following way-
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.your_Elemetnt));
In my case issue was because there is some animation, and that element is not visible for some duration. And hence exception was occurring.
For some reason I couldn't make ExpectedConditions.visibilityOfElementLocated work, so I created a code to wait for some hardcoded seconds before proceeding.
from selenium.webdriver.support.ui import WebDriverWait
def explicit_wait_predicate(abc):
return False
def explicit_wait(browser, timeout):
try:
Error = WebDriverWait(browser, timeout).until(explicit_wait_predicate)
except Exception as e:
None
And now I am calling this explicit_wait function wherever I want to wait for sometime e.g.
from selenium import webdriver
from selenium.webdriver.common.by import By
browser = webdriver.Safari()
browser.get('http://localhost')
explicit_wait(browser,5) # This will wait for 5 secs
elem_un = browser.find_element(By.ID, 'userName')

Selenium code is not working for locating an element in Safari, working in Firefox and Chrome

Can someone help who has worked on Selenium WebDriver?
I have been trying to automate a test scenario using Selenium WebDriver on a Mac machine. When I define Safari as my browser, I am getting and error "An element could not be located on the page using the given search parameters", even though that elements exists on the page in java code Issues/Bug.
Note: the same element can be located when we choose Firefox and Chrome as browser. There are some similar answers, but none of them is talking about Safari browser and Mac machine.
Try to put some wait. Use fluent wait as below :-
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS)
.pollingEvery(1, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}
});
}
The wait should work for you. If still the problem exists then use JavascriptExecutor . It will operate directly through JS. It should work. I am giving an example to click any element using JavascriptExecutor
WebElement element = driver.findElement(By.id("gbqfd"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Hope it will help you :)

Selenium WebDriver-Chrome

Hi all i am using selenium backed webdriver i am automating some third party site so i don't have any access to the code of that site problem is that my selenium test case works well firefox but when i use chromedriver it gives an exception Element is not clickable at point (693, 14). Other element would receive the click i read on some blog that using the lines of code makes the problem go the lines are given below
WebDriverWait wait=new WebDriverWait(driver, 20);
WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='sendFile']")));
element.click();
But still face same issue.
Someone please help me to resolve this issue. Thanks..
Try with JavascriptExecutor as below:-
WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id='sendFile']")));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
Hope it will work...:)

selenium IDE is successfully sending keys to hidden input box, but webdriver is throwing an error as element not visible

I have an input box which is not visible, Selenium IDE in record and playback mode is able to send keys into it and out put is successful.
same thing webdriver is throwing an error element is not visible therefore cannot be interacted with.
I have tried scripting using document.findElements.ByclassName.. there is no error but there is no output as well.
pls see the code below:
{
driver.findElement(By.cssSelector("li.cwd-clue")).click();
assertTrue(isElementPresent(By.xpath("//input[#class='cwd_input']")));
System.out.println("assert true");
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementsByClassName('cwd_input') [0].click();",tmpElement);
tmpElement.sendKeys("TELLER");}
Add WebDriverWait as shown.
WebElement ele=driver.findElement(By.xpath("//input[#class='cwd_input']");
WebDriverWait wait=new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(ele));

Resources