selenium is trowing an error as element is not visible - selenium

I have the java script injection to send text into a hidden inputbox. but webdriver is throwing an error as element is not visible so cannot be interacted with.
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(("document.getElementsByClassName('cwd_input')
[0].click();"),tmpElement);
tmpElement.sendKeys("T");

you can set value of the input via JS.
Code will be something like:
string scriptToExecute = "document.getElementById("mytext").value = 'New value';";
executor.executeScript(scriptToExecute);

Related

Changing the Span text on a UI based rich text editor(YAML) using Selenium

My UI (Hashicorp consul KV) has rich text editor which maintains a YAML file. The HTML is little complex with a mix of weird javascript code, I am trying to update the Text on a Span element using the JavascriptExecutor class however its failing,
below is the Xpath of the web element and I am changing '5000' to '100'
By number = By.xpath("//span[text()='5000'][1]");
another way of Xpath
By number = By.xpath("//span[text()='Master']/following::span[contains(text(),'child1')]/following::span[contains(text(),'child2')]/following::span[contains(text(),'child3')]/preceding::span[contains(text(),'5000')][1]");
Trial 1:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].textContent= '100';", number);
Outptut: Test Failed:Argument is of an illegal type: org.openqa.selenium.By$ByXPath
Trial 2:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById(\"//span[text()='5000'][1]\").textContent=100");
Output: Test Failed:javascript error: Cannot set properties of null (setting 'textContent')
Trial 3:
((JavascriptExecutor) driver).executeScript("arguments[0].innerHTML= '100';", number);
webEle.click();
webEle.clear();
webEle.sendKeys("100");
Output: Test Passed but the value was not set/updated
Any suggestion on how this can be achieved or if anyone has done a similar scenario? Thank you
This was possible using the Actions class - https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/interactions/Actions.html
Here is the code:
Actions builder = new Actions(driver);
WebElement TimeVal = driver.findElement(By.xpath("//span[text()='5000'][1]"));
builder.click(TimeVal).perform(); builder.moveToElement(TimeVal).clickAndHold().sendKeys(Keys.CLEAR).sendKeys("100").perform();

Data not retrieving when clicking on search button

The search button is inside the frame and I am connecting to the frame with the below code.
driver.switchTo().frame("autoCompleteDialogIF");
I am able to go the frames section.
Search button syntax:
Find
Here in the frame section I have text box and when I enter the values in text box and perform search the data is not retrieving which matches with the text.
Code used:
WebElement elementclick = driver.findElement(By.xpath(".//*[#id='filterPanelFindButton']/a"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", elementclick);
Try to click on element using java-script executor method.
WebElement elementclick = driver.findElement(By.xpath("//a[contains(text(), 'Find')]"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementclick);
OR
Try to click on element using xpath locator with Explicit wait method.
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[contains(text(), 'Find')]"))));
driver.findElement(By.xpath("//a[contains(text(), 'Find')]")).click();
I think you can just use like this:
driver.findElement(By.linkText("Find"))

Selenium click not work

Help me please.
Selenium does not click on element and element is clickable(selenium does not generate exception).
I try use Id, css, xpath locators, nothing did not help me.
What should i do to decide my problem?
Java code example.
WebElement sector = webDriver.findElement(By.id("sector-1"));
sector.click();
After click system must open this page
Seems that you try to interact with object inside a <svg> element. If so, you cannot manage it's child elements simply using click() method.
Try this instead:
WebElement svgObject = driver.findElement(By.xpath("//polygon[#id='sector-1:canvas']"));
Actions builder = new Actions(driver);
builder.click(svgObject).build().perform();
Use below XPath :
WebElement sector = webDriver.findElement(By.xpath("//g[#id='sector-1']/polygon"));
sector.click();
OR
WebElement sector = webDriver.findElement(By.xpath("//polygon[#id='sector-1:canvas']"));
sector.click();
I decide my problem.
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[#id=\"sector-2:canvas\"]")));
WebElement svgObject = webDriver.findElement(By.xpath("//*[#id=\"sector-2:canvas\"]"));
Actions builder = new Actions(webDriver);
builder.click(svgObject).build().perform();

Selenium is showing element not found exception

I am able to do validation on first page using selenium. But when i am transferring to next page than it is showing above error.
Code is:-
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://192.168.1.47";
Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl);
driver.get(baseUrl);
selenium.open("http://192.168.1.47");
selenium.type("username","clnt123822");
selenium.type("birthday", "150347");
String name = driver.findElement(By.name("username")).getAttribute("value").toString();
String dob = driver.findElement(By.name("birthday")).getAttribute("value").toString();
System.out.println(name);
System.out.println(dob);
selenium.click("btnsubmit");
// After pressing this button it is going to next page where i am having select
html tag.At this point error is coming
Exception in thread "main" com.thoughtworks.selenium.SeleniumException: Element select1 not found
This is second page checking which is throwing error
selenium.type("select1","r");
String select1 = driver.findElement(By.id("sel1")).getAttribute("value").toString();
System.out.println(select1);
I guess you need to wait until the element in the second page to be visible/clickable. Try this code just after hitting the submit button in page one.
WebElement element = new WebDriverWait(Driver,30).until(ExpectedConditions.elementToBeClickable(By.id("sel1")));

How to locate elements on a javascript form using Selenium WebDriver

I have a page that renders a login form. Using Firefox's firebug I have identified the username id = txtUsername and the password text box is txtPassword. Everything is fine in Firefox. BUT moving over to IE Explorer browser, am getting error and cant locate the elements.
On inspecting the page, I found the form is being rendered by a JavaScript function. Below is the html:
form name="frmLogin" id="frmLogin" onsubmit="return $('#frmLogon').valid();" action="default.aspx" method="post" jQuery172043="1" novalidate="novalidate"
How can I locate the page elements using selenium webdriver.
This is the code I have written.
/* setup for IE. Remove if not needed */
File file = new File("C:/SeleniumJavaLibrary/selenium-2.28.0/IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability("ignoreZoomSetting", true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.manage().timeouts().implicitlyWait(28,TimeUnit.SECONDS );
/*setup for IE*/
driver.get("http://website.com");
WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('#frmLogin').valid();");
element.findElement(By.name("txtUsername")).sendKeys("user");
element.findElement(By.name("txtPassword")).sendKeys("password");
I would do the following:
WebDriverWait waiting = new WebDriverWait(driver, 15, 100);
WebElement element = waiting.until(ExpectedConditions.visibilityOfElementLocated(By.id("frmLogin")));
This will create a WebDriverWait object that will wait up to 15 seconds, checking to see if the expected condition is valid every 100 milliseconds. This object is then used to wait until the frmLogin element is visible on the page, and then returns you a WebElement that can be used later on in your test (if required).