Crickcoder
Crickcoder

Reputation: 2155

How to change the style of an element using selenium

I have an image in my code. When I click on it, I want its style to be changed. The field looks like this :

<a class="myField" href="#" style="left: 0%;"></a>

I can get the element by class and click on it. But I don't know how to change the style.

WebElement webelement = driver.findElement(By.className("myField"));
webelement.click();
//Change the style to 'left:40%;'

I am new to selenium. Any help appreciated.

Upvotes: 0

Views: 6317

Answers (1)

Richard
Richard

Reputation: 9019

You can do that using javascriptExecutor:

((JavascriptExecutor)driver).executeScript("document.getElementsByName('myField')[0].style.left='40%'");

Upvotes: 1

Related Questions