Selenium Java - mark checkbox selected without using .click() - java

I want mark some checkboxes as checked using Selenium and Java, but in the .css style sheet their "width" and "height" is set to "100", yet in the browser they appear as normal checkboxes. Because of this selenium finds them and succesfully executes .click() function, but the checkbox does not get selected.
Is there a way to simply set the checkbox as selected without using .click() ?

Difficult to say without a reproducible sample, but you may try clicking via javascript:
WebElement checkbox = driver.findElement(By.ID("mycheckbox"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", checkbox);
See here the differences:
WebDriver click() vs JavaScript click()

Im afraid there is no select() method on a check box,
but you could write something like this and reuse it.. which will abstract the operation of select
if ( !driver.findElement(By.id("idOfTheElement")).isSelected() )
{
driver.findElement(By.id("idOfTheElement")).click();
}

Related

click on a button with JQuery

There is somthing wrong with the page I want to test.
My first try:
When I clicked manually on a button, then I will be forwarded normally on the next page.
When I tried to click on the same button with selenium, then I get an error page "Sorry...something gone wrong...blabla". I think this problem can only solve the developer team of the page.
By book = By.cssSelector("#button\\.buchung\\.continue");
//By book = By.cssSelector("button.buchung.continue");
//By book = By.xpath("//*[#id='button.buchung.continue']");
WebElement element= ConfigClass.driver.findElement(book);
element.click();
But I want to try a workaround:
I clicked on the same button with JQuery.
I opened my chrome console and execute the button with:
jQuery('#button\\.buchung\\.continue').click()
How can I execute this JQuery expression in my selenium code?
I tried this, but without success:
JavascriptExecutor je = (JavascriptExecutor) driver;
je.executeScript("jQuery('#button\\.buchung\\.continue').click()");
Use $
je.executeScript("$('#button\\.buchung\\.continue').click()");
jQuery("selector") will return you a list.
I think you have to call click() on the element at index 0 (Assuming exactly one element satisfies the selector)
Code:
je.executeScript("jQuery('#button\\.buchung\\.continue')[0].click()");
You were pretty close. If the cssSelector is uniquely identifying the WebElement you can use the following code block :
By book = By.cssSelector("#button\\.buchung\\.continue");
WebElement element= ConfigClass.driver.findElement(book);
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

Can't Select Checkbox, Selenium WebDriver

I am unable to select a checkbox with Selenium WebDriver in Java.
I tried by Xpath but no result.
WebDriver can't click on element.
I tried with Selenium IDE - recorder, no results.
Here it is - html code for checkbox
I try:
1.
driver.findElement(By.xpath(".//form[#id='placeOrderForm1']/div[#class='terms right']/label")).click();
2.
driver.findElement(By.id("Terms1")).click();
3.
driver.findElement(By.cssSelector("label")).click();
4.
driver.findElement(By.xpath("//div[3]/form/div/input")).click();
Nothing works.
Please help.
Try using JavascriptExecuter Hope this will help
WebElement element = driver.findElement(By.id("Terms1"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].click();", element );
Your code seems correct. Specially this one -
driver.findElement(By.id("Terms1")).click();
It might be possible the element you are clicking is not visible in the page scroll. Try to move to the element first and then click.
Try with this -
WebElement elem = driver.findElement(By.id("Term1"));
Actions action = new Actions(driver).
action.moveToElement(elem).click().build().perform();
Hope this help.
Here is the Answer of your Question:
As you mentioned unable to select a checkbox, actually we don't select the checkbox, we checkmark the checkbox. The checkbox you depicted have an id as Terms1 and name astermsCheck`. So you use either of the locators to checkmark the checkbox as follows:
driver.findElement(By.id("Terms1")).click();
OR
element = driver.findElement(By.name("termsCheck")).click();
Let me know if this Answers your Question.
You can find the element by a unique identifier. In this case, we can use name or id. The better choice is to go with id.
WebElement element = driver.findElement(By.name("termsCheck"));
element.click();
or you can use this one also
driver.findElement(By.id("Terms1")).click();

How many ways to click on webElement In WebDriver?

As I am aware that user can click on Particular Webelement by using click method and one more way like using Sendkey Method with ASCII Value for left Click.
By Click Method: driver.findElement(By.cssSelector(".dbl")).click();
By Ascii Value : driver.findElement(By.cssSelector(".dbl")).sendKey("ASCII VALUE FOR Left Click");
Apart from this is there a way to perform click action??
You can use:
yourelement.sendKeys(Keys.RETURN) or .sendKeys(Keys.ENTER) : which is an equivalent of focusing that element and hitting RETURN/ENTER on that element
Also, There are methods to do this using Javacript but it is not usually recommended:
using the non-native Javascript Executor:
((JavascriptExecutor) driver).executeScript("arguments[0].click();", yourelement);
or by using Javascript Library:
JavascriptLibrary jsLib = new JavascriptLibrary();`
jsLib.callEmbeddedSelenium(driver, "triggerMouseEventAt", we, "click", "0,0");
Below are some methods that will be useful to click a button/Image.
WebDriver driver = new ChromeDriver();
driver.get("http://newtours.demoaut.com");
WebElement signOnImage = driver.findElement(By.xpath("//input[#type='image'][#name='login']"));
// direct method from the API which is recommended always
signOnImage.click();
1 Using Return Key
//signOnImage.sendKeys(Keys.RETURN);
2 Using JavascriptExecutor
2.1
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", signOnImage);
2.2
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementsByName('login')[0].click()");
3 Using Actions class
3.1
Actions actions = new Actions(driver);
actions.click(signOnImage).perform();
3.2
Actions actions = new Actions(driver);
actions.moveToElement(signOnImage).click().perform();
3.3
Actions actions = new Actions(driver);
actions.clickAndHold(signOnImage).release().perform();
3.4
Actions actions = new Actions(driver);
actions.sendKeys(signOnImage, Keys.RETURN).perform();
submit();
If the current element is a form, or an element within a form, then this will be submitted to the remote server. If this causes the current page to change, then this method will block until the new page is loaded
There are four typical ways to perform click in Selenium-Java bindings.
Using findElement
driver.findElement(By.xpath("//span[text()='Excel']")).click();
Using WebDriverWait
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Excel']"))).click();
Using executeScript
WebElement button = driver.findElement(By.xpath("//span[text()='Excel']"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", button);
Using ActionClass
WebElement button = driver.findElement(By.xpath("//span[text()='Excel']/parent::button[#aria-controls='report'][contains(#class,'downloadExcel')]"));
new Actions(driver).moveToElement(button).click().build().perform();
I am using xpath, you can use css, linkText, tagName, name, partialLinkText etc as well to perform click.
If you want to click a button or set an value to an web element through selenium you can use XPATH variable ,for using XPATH variable you must find the value of it ,you can find it using Firefox browser and few add_on's like firebugs .
driver.findElement(By.xpath(".//*[#id='main']/div[4]/div/button")).click();
I would suggest you to use XPATH variable so that you can locate any web element in a webpage.
If you want find the hyperlink web element then you can use By.linkText when you are sure about the tag name or choose By.partialLinkText by which you can locate even if you partial web element name but in this case your partial search key matches more than one element then By.partialLinkText won't works fine.
For example,in case you knows the complete tag name of hyperlink use can use
driver.findElement(By.linkText("Click to Next Page")).click();
or else
Where you knows only a partial tag name
driver.findElement(By.linkText("Next Page")).click();
The secound option will not help you in all instances.
HTMLElement.click()
The HTMLElement.click() method simulates a mouse click on an element. At the core level when click() is used with supported elements e.g. <a>, <button>, <input>, etc, it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.
There are several ways to invoke the click() method as follows:
Element click(): This Element Click command scrolls into view the element if it is not already pointer-interactable, and clicks its in-view center point. An example:
driver.findElement(By.linkText("Selenium")).click();
Element sendKeys(Keys.SPACE): An example:
driver.findElement(By.linkText("Selenium")).sendKeys(Keys.SPACE);
Actions click(): Clicks at the current mouse location. Useful when combined with moveToElement(org.openqa.selenium.WebElement, int, int) or moveByOffset(int, int). An example:
new Actions(driver).click(element).build().perform();
Actions doubleClick(): Performs a double-click at the current mouse location. An example:
new Actions(driver).keyDown(Keys.CONTROL).doubleClick(link).keyUp(Keys.CONTROL).build().perform();
Action sendKeys(textToSend, Keys.RETURN): An example:
new Actions(driver).moveToElement(element).sendKeys(textToSend, Keys.RETURN).perform();
Action sendKeys(textToSend, Keys.ENTER): An example:
new Actions(driver).moveToElement(element).sendKeys(textToSend, Keys.ENTER).perform();
Actions contextClick(): Performs a context-click at the current mouse location. An example:
new Actions(driver).contextClick(element).build().perform();
JavascriptExecutor executeScript​(): Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.
((JavascriptExecutor)driver).executeScript("arguments[0].click()", webElement);
Submit(): Applicable if this current element is a form, or an element within a form.
WebElement searchButton = driver.findElement(By.xpath("//button[#type='submit']"));
searchButton.submit();
Mouse Double Click
Mouse Triple Click

How to remove a web-element using in selenium using Java?

Is there any way by which we can remove a web-element from the the source of the page.
I have the problem that there is a division of popup in the webpage source which is disabling all other divisions. If we remove this division while doing inspect element from a browser all other elements are automatically enabled for use.
How can I do this in selenium webdriver?
To my knowledge, there is no direct Java method, but you can use the JavaScriptExecutor API to do something like:
WebElement yourElement = …
JavaScriptExecutor jsExecutor = (JavaScriptExecutor) webDriver;
jsExecutor.executeScript(
"arguments[0].parentNode.removeChild(arguments[0])", yourElement);

Java selenium click element not working

I want to click the load More link in a page. My code is below.
pageUrl="http://www.foundpix.com/category/actor/bollywood-actor/"
WebDriver driver = new FirefoxDriver();
driver.get(pageUrl);
driver.manage().window().maximize();
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("window.scrollBy(0,2500)", "");
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("json_click_handler")));
driver.findElement(By.id("json_click_handler")).click();
How can I make it click the link.
You can use below xpath to click Load More button both the times:-
driver.findElement(By.xpath("//*[#id='blocks-left']/div/div[3]/div[contains(.,'Load More')]")).click();
this button change the location after you click on it and it can be clicked twice so:
before the first click use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div")).click();
after first click,you can use
driver.findElement(By.xpath("//*[#id="blocks-left"]/div/div[3]/div[2]")).click();
Maybe come at it from a different angle. Do you really need to have the link clicked or do you have some Javascript function that gets called on click of the link (like window.loadMore). Can you call the function directly? Selenium is a bit annoying in the sense you can only click a visible element (I don't mean it has to be in the viewport - it just can't have a style like display:none;).

Categories