简体   繁体   English

如何使用 Java 在 Selenium 中按 Ctrl + Shift

[英]How to press Ctrl + Shift in Selenium with Java

I'm trying to change the text direction in the google search box from (right-to-left) to (left-to-right) using selenium before actually sending keys to it.在实际向其发送键之前,我正在尝试使用 selenium 将 google 搜索框中的文本方向从(从右到左)更改为(从左到右)。

Google automatically sets the direction (right-to-left) because of my location and language.由于我的位置和语言,Google 会自动设置方向(从右到左)。

i tried this code我试过这个代码

driver.get("https://www.google.com.eg/");
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").perform();

But no matter what it doesn't work i also tried :但不管它不起作用我也试过:

box.sendKeys(Keys.chord(Keys.LEFT_CONTROL, Keys.LEFT_SHIFT));

I can't seem to find a way in Selenium to press only 2 modifier keys without letters and get results我似乎无法在 Selenium 中找到只按 2 个没有字母的修饰键并获得结果的方法

Try adding .build() so that your code will be尝试添加.build()以便您的代码

driver.get("https://www.google.com.eg/");
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").build().perform();

And if this still not work try adding an explicit wait如果这仍然不起作用,请尝试添加显式等待

WebDriverWait wait = new WebDriverWait(driver, 20);
driver.get("https://www.google.com.eg/");
wait.until(ExpectedConditions.elementToBeClickable(By.name("q")));
WebElement box = driver.findElement(By.name("q"));
Actions actions = new Actions(driver);
box.click();
actions.keyDown(Keys.LEFT_CONTROL).sendKeys(Keys.LEFT_SHIFT).keyUp(Keys.LEFT_CONTROL).sendKeys("abcd").build().perform();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM