1

Selenium version 4.7.2 I use Anaconda and installed Selenium over Anaconda wit conda command.

How to press left arrow in Selenium?

My project has below libraries

from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import random
import time

Code:

I use this code structure it works, it clicks, but i want Selenium to press right arrow on keyboard. How can i do this?

next = browser.find_element(By.XPATH, "//button[@class='_abl-']//div[@class='_abm0']//*[@aria-label='Next']")
next.click()

I make a search and found this. Arrow Key – Right Keys.ARROW_RIGHT

But i have no idea about coding it.

Thanks for your help Thanks for all comments

2 Answers2

0

To press <- key once you have to use the ActionChains() involving key_down(Keys.ARROW_LEFT) method followed by key_up(Keys.ARROW_LEFT) method as follows:

from selenium.webdriver.common.action_chains import ActionChains

ActionChains(browser).key_down(Keys.ARROW_LEFT).key_up(Keys.ARROW_LEFT).perform()

tl; dr

Link to documentations:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Thanks When i try from selenium.webdriver.common.action_chains import ActionChains ActionChains(driver).key_down(Keys.ARROW_LEFT).key_up(Keys.ARROW_LEFT).perform() driver is underlined and I got an error 'driver is not defined' How can i fix this? Thanks –  Feb 13 '23 at 08:15
  • 1
    Thanks very much ActionChains(browser).key_down(Keys.ARROW_LEFT).key_up(Keys.ARROW_LEFT).perform() worked for me –  Feb 13 '23 at 10:16
0

You can try the below code to simulate arrow keys:

from selenium.webdriver import Keys

for Left Arrow:

<WebElement>.send_keys(Keys.ARROW_LEFT)

for Right Arrow:

<WebElement>.send_keys(Keys.ARROW_RIGHT)
AbiSaran
  • 2,538
  • 3
  • 9
  • 15