Here is my css Selector codes
driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='append_numbers']")).click();
driver.findElement(By.cssSelector("button[class='btn-link'][data-sugg-technik='make_pairs']")).click();
They are working, and I want to focus these elements with Selenium. How can I do this?
Edit:
js.executeScript("document.querySelector('button,btn-link,make_pairs).focus();");
And also this code is working, but always focuses the first item, which is not what I want.
<li>
<button class="btn-link" data-sugg-sources="email,full_name" data-sugg-technik="make_pairs">AlZehraala16</button>
</li>
I want to focus just this button.
there are two say you can do it. You can do it using webdriver code or javascript. I guess using webdriver is much better as it uses native events. Following code shows how to do using webdriver.
@Test
public void focusElement(){
WebDriver driver = new FirefoxDriver();
driver.get("https://google.com");
driver.manage().window().setSize(new Dimension(1000,300));
WebElement searchBox = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
actions.moveToElement(searchBox).build().perform();
}
if you want to do that using JavaScript . This is how you need to modify your code.
public void focusElement(WebElement element){
if (driver instanceof JavascriptExecutor) {
((JavascriptExecutor) driver).executeScript("arguments[0].focus();", element);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.