Home > Software design >  Using arrow keys on Chrome Selenium popup Python
Using arrow keys on Chrome Selenium popup Python

Time:12-27

I'd like to use the scrollbar on the popups from the below page. If you click any product on the page it will open a popup where you can add extra items. I'm trying to use Keys.send_keys(Keys.ARROW_DOWN) to scroll down the popup but I can't find the scrollbar element in Chrome. I've tried moving to elements using other methods without success so would like to try with the arrow keys.

https://www.just-eat.co.uk/restaurants-mcdonalds-victorialondon/menu

I managed to use the arrow keys on the popups in FireFox but can't replicate in Chrome.

CodePudding user response:

I like using JavaScript to scroll down when using Selenium. It's a lot more reliable from my experience.

Try the following:

X = 500
DRIVER_NAME.execute_script("window.scrollBy(0, X)")

Another option is to circumvent the need to scroll in the first place by injecting CSS into the webpage. You can write several lines of CSS code that would make the popup's font and line-height very small; this way, you'll be able to fit everything into the screen without scrolling.

Here's how that would look like:

new_css = "body{background-color: white;}"
DRIVER_NAME.executeScript("$('<style type=\"text/css\">new_css</style>').appendTo('html > head');");

One of the two solutions above should solve your problem.

CodePudding user response:

You can try to find an element inside the popup then use that element to scroll. The trick here is to make sure the element is focusable so that you can use that for scrolling using arrow keys. focusable elements are the ones that can receive keyboard events, the elements that has a focus function declared inside it. Since the w3c documentation http://www.w3.org/TR/DOM-Level-2-HTML/html.html is not updated for a long time, we don't have a list of focusable elements, but some of the focusable elements according to it are HTMLInputElement, HTMLSelectElement, HTMLTextAreaElement, HTMLIFrameElement, HTMLButtonElement, HTMLAnchorElement and any element with a tabindex.

Following is a sample code snippet for your reference.

from selenium.webdriver.common.keys import Keys

focusable_element_in_popup = driver.find_element_by_id('id_of_the_element')
focusable_element_in_popup.send_keys(Keys.ARROW_DOWN)

#or you can use //a magic using xpath to return you the any first link on popup, like below

#driver.find_element_by_xpath('//div[@]//a') 

or you can also use move_to_element or scrollIntoView as below. If this doesn't work as expected try adding actions.click() after move_to_element to focus it.

from selenium.webdriver.common.action_chains import ActionChains

element = driver.find_element_by_id("my-id")

actions = ActionChains(driver)
actions.move_to_element(element).perform()

#actions.move_to_element(element)
#actions.click()
#actions.send_keys("SOME DATA")
#actions.perform()

#or 

driver.execute_script("arguments[0].scrollIntoView();", element)

Some trial and error might be required at first to get it right for your use case.

  • Related