0

HTML: <input name="txtAnswer" class="box1" id="txtAnswer" type="text" maxlength="20">

Element can be selected using Firefox IDE: css=tr:nth-child(2) > td > table > tbody > tr:nth-child(3) > td:nth-child(1)

I need to convert css that appear Firefox IDE to path can used in my code

driver.findElement(By.name("txtAnswer")).sendKeys("green");

Marvin Klar
  • 1,869
  • 3
  • 14
  • 32
Ali Sultan
  • 13
  • 6

1 Answers1

0

Try using JavascriptExecutor

JavascriptExecutor javascriptExecutor = (JavascriptExecutor) driver;
WebElement element = driver.findElement(By.name("txtAnswer"));
js.executeScript("arguments[0].setAttribute('style', 'colour:green')", element);

It's better to use findElement by unique id rather than by name.

More details here: How can i set new style of element using selenium web-driver

Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
  • thanks for you replay also view same error "cannot locate an element using id=txtAnswer". i tried with may syntax but all view error. the scenario is login form have username and password , after i enter valid username and password and press on button login view page have security question this page that i can't inspect element at it. but it use selenium IDE it located the element – Ali Sultan Apr 17 '19 at 10:40
  • @AliSultan If you are referring by `id` attribute you need to specify that in your HTML like `` and `driver.findElement(By.id("txtAnswer"));` or for time being use `driver.findElement(By.name("txtAnswer"));` – Mebin Joe Apr 17 '19 at 12:12
  • this is my code but it not work: driver.findElement(By.id("txtAnswer")).click(); driver.findElement(By.id("txtAnswer")).sendKeys("green"); maybe i need to wait after page open to access element. – Ali Sultan Apr 18 '19 at 06:57
  • @AliSultan wait for the element to be loaded in DOM. `WebElement element = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.elementToBeClickable(By.id("txtAnswer")));` – Mebin Joe Apr 18 '19 at 07:02
  • thanks but it view same error (Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for element to be clickable: By.id: txtAnswer (tried for 10 second(s) with 500 milliseconds interval) at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) at Demo.TasheelIE.main(TasheelIE.java:39) Caused by: org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"txtAnswer"} – Ali Sultan Apr 21 '19 at 05:23
  • this is my code: System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); driver = new ChromeDriver(); // Maximize the browser window driver.manage().window().maximize(); // navigate to specified url driver.get("http://dxbqcapp01/molforms/login.aspx"); driver.findElement(By.id("txtUserName")).sendKeys("MS200963915"); driver.findElement(By.id("txtPassword")).sendKeys("test@123"); driver.findElement(By.xpath("//input[@type='submit' and @value='Submit']")).sendKeys(Keys.ENTER); driver.findElement(By.id("txtAnswer")).sendKeys("green"); – Ali Sultan Apr 21 '19 at 05:31
  • @AliSultan did you try increasing the timeout. – Mebin Joe Apr 22 '19 at 04:19