0

hi when we have to change tags attribute value with the help of javascriptexecutor in selenium webdriver (java) we use like belwo :

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('//id of element').setAttribute('attr', '10')");

and elements attribute is changed.

Also while automating a website i have to click a button i have used normal webdriver

driver.findElement(By.xpath("//*[data-dismiss='modal']").click();

to click it but always i got Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: element not visible. the html source for that button is

<button class="indigo-submit btnStyle clsmdl" data-dismiss="modal" aria-hidden="true">Close</button>

also i have noticed that if i use Actions of selenium over the button i get no exception but even no click at the button i.e my script pass with no exception but also it does not click. after doing a lot search what i found that

<body style="overflow: hidden;">
...
        ...
        < <button class="indigo-submit btnStyle clsmdl" data-dismiss="modal" aria-hidden="true">Close</button>
        ...
...
</body>

Style attribute of body tag is "overflow:hidden" which as per How to check what is causing a web element to be invisible to Python Selenium? link Some users say Selenium cannot click a web element which is under a parent node with overflow: hidden.Also i have tried the suggestion provided in the link but no luck ,may be i am not able to convert python code into java correctly.

1.Now Please help me how can i use javascriptexceutor to change the body style tag to 
  visible.
2.Please help me how can i use javascriptexceutor to change the button aria-hidden tag to 
  false. Also i want a way to verify the same.
Community
  • 1
  • 1
Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39

2 Answers2

1

@raj N ish Ku M ar, i give the answer here, i close ur pop up in my machine like below:

Element Not visible error

Community
  • 1
  • 1
noor
  • 2,954
  • 2
  • 16
  • 29
  • @ Noor thanks its working but my question was how to 1.Now Please help me how can i use javascriptexceutor to change the body style tag to visible. 2.Please help me how can i use javascriptexceutor to change the button aria-hidden tag to false.so that i can click on the button – Rajnish Kumar Apr 29 '16 at 14:43
  • Also plz explain why it is necessary to keep a space in between **emailItinerarySuccessModal** and **dot**.cause when i run it without space it do not worked – Rajnish Kumar Apr 29 '16 at 14:50
  • $("#ur id").css('display','none'); or $("#ur id").attr("aria-hidden", false); or $(".ur class").attr("aria-hidden", false); actually all those are jquery methods. first i have tried by making ur aria-hidden attribute false but its not worked thats why i take ur modal root element. and space after id has a meaning that means that class after id like cssSelector for ex div:nth-child(3) – noor Apr 29 '16 at 15:27
  • thanks @ noor for the explanation but my basic aim was not to click on the dialog box ,my basic question was how can we with the help of java script can change the body style and attribute aria-hidden to visible/false and after that how to verify that if it actually got changed or not ,also to add i have already up-voted your answer as indeed its half the solution – Rajnish Kumar Apr 29 '16 at 16:36
0

I'm also not sure that I can convert Python to Java correctly :) Try following:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementsByClassName('indigo-submit btnStyle clsmdl')[0].aria-hidden='false'");

If you want to change element style use JavaScript like this:

document.getElementsByClassName('indigo-submit btnStyle clsmdl')[0].style.visibility="visible";
Andersson
  • 51,635
  • 17
  • 77
  • 129
  • @ Andersson thanks for the quick reply after using your solution i am getting below exception Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Runtime.evaluate threw exception: ReferenceError: Invalid left-hand side in assignment – Rajnish Kumar Apr 29 '16 at 11:37
  • looks like above script is not valid :) – Rajnish Kumar Apr 29 '16 at 11:39
  • Hi JavaScriptExecutor is an interface which provides mechanism to execute Javascript through selenium driver. It provides “executescript” & "executeAsyncScript" methods, to run JavaScript in the context of the currently selected frame or window. – Rajnish Kumar Apr 29 '16 at 12:07