4

I would like click and hold the bar in a webpage and move it to the right.

Here is my code:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

browser = webdriver.Ie('IEDriverServer.exe')
url = 'https://verify.meituan.com/v2/web/general_page?action=spiderindefence&requestCode=507e224b8f0f447793ad3a03830984c8&platform=1000&adaptor=auto&succCallbackUrl=https%3A%2F%2Foptimus-mtsi.meituan.com%2Foptimus%2FverifyResult%3ForiginUrl%3Dhttp%253A%252F%252Fwww.dianping.com%252Fsearch%252Fkeyword%252F1%252F0_%2525E5%252590%25258D%2525E5%252588%25259B%2525E4%2525BC%252598%2525E5%252593%252581%252F10&theme=dianping'
browser.get(url)
slider = browser.find_element_by_xpath("//div[@id='yodaBoxWrapper']//div[@id='yodaMoveingBar']")
ActionChains(browser).click_and_hold(slider).move_by_offset(xoffset=50, yoffset=0).perform()

The bar does not move.

What's wrong with my code?

Thanks a lot.

Chan
  • 3,605
  • 9
  • 29
  • 60

1 Answers1

0

In your code you are trying to do some opeartion on web element, if you see manually how site loads than you will find that it takes time to render HTML components on page.

You need to wait until element is rendered on the page and an extra check to make sure its clickable which you can do with below code. For full list of best practices please check this answer.

slider = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@id='yodaBoxWrapper']//div[@id='yodaMoveingBar']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='yodaBoxWrapper']//div[@id='yodaMoveingBar']")))
ActionChains(browser).click_and_hold(slider).move_by_offset(xoffset=200, yoffset=0).perform()

Below is the full code

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

browser = webdriver.Ie('IEDriverServer.exe')
url = 'https://verify.meituan.com/v2/web/general_page?action=spiderindefence&requestCode=507e224b8f0f447793ad3a03830984c8&platform=1000&adaptor=auto&succCallbackUrl=https%3A%2F%2Foptimus-mtsi.meituan.com%2Foptimus%2FverifyResult%3ForiginUrl%3Dhttp%253A%252F%252Fwww.dianping.com%252Fsearch%252Fkeyword%252F1%252F0_%2525E5%252590%25258D%2525E5%252588%25259B%2525E4%2525BC%252598%2525E5%252593%252581%252F10&theme=dianping'
browser.maximize_window()
browser.get(url)
slider = WebDriverWait(browser, 30).until(EC.presence_of_element_located((By.XPATH, "//div[@id='yodaBoxWrapper']//div[@id='yodaMoveingBar']")))
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='yodaBoxWrapper']//div[@id='yodaMoveingBar']")))
ActionChains(browser).click_and_hold(slider).move_by_offset(xoffset=200, yoffset=0).perform()
Sariq Shaikh
  • 940
  • 8
  • 29
  • Forgot to mention that I have used `IE 11` and IE driver version `3.150.1.0 (32 bit)` to test this and it works fine in it, if you can elaborate are you getting any errors ? – Sariq Shaikh Nov 25 '20 at 10:52
  • I use IE 11. The code finished successfully but the bar does not move – Chan Nov 25 '20 at 11:14