我想选择一些文字并执行点击操作 - 例如在Winword中,我们在选择了一些文字后点击Bold
...
我必须选择文字,然后点击<B>
中的textarea
粗体图标。
有关如何使用Selenium / Webdriver进行此操作的想法吗?
答案 0 :(得分:8)
在Java中,The Advanced User Interactions API有你的答案。
// the element containing the text
WebElement element = driver.findElement(By.id("text"));
// assuming driver is a well behaving WebDriver
Actions actions = new Actions(driver);
// and some variation of this:
actions.moveToElement(element, 10, 5)
.clickAndHold()
.moveByOffset(30, 0)
.release()
.perform();
答案 1 :(得分:0)
我试过这种方式并没有用。以下是代码:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
Actions actions = new Actions(driver);
actions.moveToElement(text, 10, 5).clickAndHold().moveByOffset(30, 0).release().perform();
我切换到JavascriptExecutor并且工作正常:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: blue;');", text);
答案 2 :(得分:0)
我尝试使用Action
builder
并使用offset
进行游戏。它对我有用。
Actions action = new Actions(driver);
action.moveToElement(wblmt,3,3).click().keyDown(Keys.SHIFT).moveToElement(wblmt,200, 0).click().keyUp(Keys.SHIFT).build().perform();