Selenium WebDriver-Chrome - selenium-webdriver

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...:)

Related

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

Explicit wait is not working for Firefox (52.4.0 (64-bit))

Explicit wait is not working for Firefox (52.4.0 (64-bit))below is my code:
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://XXXX/XXXXX/XX/login");
driver.findElement(By.id("userId")).sendKeys("XXXXX");
driver.findElement(By.id("password")).sendKeys("XXXXX");
driver.findElement(By.id("submit")).click();
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[text()='Viewer']")));
driver.findElement(By.xpath("//span[text()='Viewer']")).click();
}
I have to use Explicit wait here in any case as elements doesn't get loaded in fixed time. I have searched lot in google but didn't find any code working for me.
As per your code attempt it seems you are waiting for the WebElement through WebDriverWait and then trying to invoke click() method. In your code you have used the clause presenceOfElementLocated with ExpectedConditions, which as per the documentation doesn't confirms if the WebElement is Displayed and Enabled as well.
A better solution will be to modify the clause for ExpectedConditions where instead of presenceOfElementLocated we have to use elementToBeClickable as follows:
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Viewer']")));
You are using presenceOfElementLocated. As per documentation:
An expectation for checking that an element is present on the DOM of a
page. This does not necessarily mean that the element is visible.
You have to be sure that you are waiting on the element the correct expected condition.
as per #DebanjanB , i just it is not an good idea to use presenceOfElementLocated instead it is always better to use elementToBeClickable so correct code should be:
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "C:\\Drivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://XXXX/XXXXX/XX/login");
driver.findElement(By.id("userId")).sendKeys("XXXXX");
driver.findElement(By.id("password")).sendKeys("XXXXX");
driver.findElement(By.id("submit")).click();
WebDriverWait wait = new WebDriverWait(driver, 50);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Viewer']")));
driver.findElement(By.xpath("//span[text()='Viewer']")).click();
}

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

presenceOfElementLocated in selenium unable to locate presence of Webelemnt

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

Resources