How to remove a web-element using in selenium using Java? - 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);

Related

Clicking buttons with Selenium WebDriver Java

There is a button on a website I am testing and Selenium can't seem to click it. I have tried many different ways but still nothing. Attached is the HTML for the button as well as the XPath.
Has anyone else experienced this or knows how to get around this?
XPath:
/html/body/div[1]/div/div/form/div[21]/div[3]/button
[HTML]
Could be a synchronization issue. kindly try below solution if your element is not within iframe or else you need to switch control to iframe first before dealing with the web element.
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"))).click();
Or You can also try javascript solution:
WebElement element= driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Try below code -
Actions action = new Actions(driver);
WebElement My_btn = webdriver.findElement(By.xpath("/html/body/div[1]/div/div/form/div[21]/div[3]/button"));
action.moveToElement(My_btn).click(My_btn).build().perform();
Let me know the outcome.

How to write the selenium code for scroll down the mouse action, I will try with this below code it was

How to write the selenium code for Scroll down the mouse below. I try to use the below code it was not scrolling action.
JavascriptExecutor Scrool = (JavascriptExecutor) driver;
Scrool.executeScript("window.scrollBy(0,250)", "");
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
use this method. Call this method with actual webelement where you would like to scroll
public void scrollToElement(WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
}
#tester
so, first of all, you need to be careful with automation. Before to start the automation check website and be sure that there are no iframes. in your case on your website, you have IFRAME that's why you are getting no such element exception which means your code not reaching this web element.
so to handle this problem you can run it in 2 ways.
the first way to scroll to webelement which you need
driver.get("https://www.abservetechdemo.com/products/rebueats/");
driver.switchTo().frame(0);
WebElement scrollToelement = driver.findElement(By.xpath(" //h2[contains(text(),'Your restaurant, delivered')]"));
((JavascriptExecutor) driver.executeScript("arguments[0].scrollIntoView(true);", scrollToelement);
and the second way just uses your own code but add this driver.switchTo.iframe(0). in your case, you have only 1 iframe so 0 means index of the iframe.
driver.get("https://www.abservetechdemo.com/products/rebueats/");
driver.switchTo().frame(0);
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scrollBy(0,2500)");
I am sure this will solve your issue I test it couple times

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

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

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

How to use javascript to set attribute of selected web element using selenium Webdriver using java?

I want to use javascript to set attribute for selected element on webpage.
I have found 2 ways to set attribute using javascript
1
WebDriver driver; // Assigned elsewhere
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementByID('//id of element').setAttribute('attr', '10')");
2
WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element);
But I want to apply javascript to specific webelement which i have found using selenium webdriver
as an example i have select one link using selenium webdriver
driver.findElement(By.linkText("Click ME"))
Now I want to set attribute of this webelement using javascript
but I don't know how to combine both
please help me to find solution
Along the lines of:
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.linkText("Click ME"));
js.executeScript("arguments[0].setAttribute('attr', '10')",element);
I have also faced a similar issue and I have used the javascript Executor
so in my case, I have a list of elements on which I have to change one property
here first I am finding the element, then traversing through the list, and creating a javascriptExecutor object and then executing the script on that particular element
//arguments[0] means the element
//arguments[1] means the property
//arguments[2] means the new value of the propert
List<WebElement> unselectableDiv = driver
.findElements(By.xpath("//div[#class='x-grid3-cell-inner x-grid3-col-6']"));
for (WebElement element : unselectableDiv) {
// System.out.println( "**** Checking the size of div "+unselectableDiv.size());
JavascriptExecutor js = (JavascriptExecutor) driver;
String scriptSetAttr = "arguments[0].setAttribute(arguments[1],arguments[2])";
js.executeScript(scriptSetAttr, element, "unselectable", "off");
System.out.println(" ***** check value of Div property " + element.getAttribute("unselectable"));
}
As per your code trials:
driver.findElement(By.linkText("Click ME"))
The innerHTML seems to be set as Click ME.
So, to set a new value e.g. 10 as the innerHTML you can use the executeScript() method of the JavascriptExecutor interface and you can use the following solution:
Using innerHTML:
WebDriver driver;
WebElement element = driver.findElement(By.linkText("Click ME"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].setAttribute('innerHTML', '10')", element);
Ideally, you need to you need to induce WebDriverWait for the elementToBeClickable() and and you can use the following solution:
Using textContent:
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.linkText("Click ME")))
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('textContent','10')", element);
Reference
You can find a relevant detailed discussion in:
Selenium Datepicker using JavascriptExecutor

Categories