最近练手写一个基于某购物网站的自动下单软件,然后在购物篮中选择商品时报错:
WebDriverException: Message: Element is not clickable at point (872, 654). Other element would receive the click
在网上搜了一下,大概就是因为页面还没有显示该按钮,导致click失败,
解决办法有两个:
1、用selenium +js的方式 (我尝试了,但是失败了)
2、拖动页面到该按钮处再点击(我使用了此办法)
详细代码实现如下:
#首先定位到该元素,shopname为商品名字
shop = driver.find_element_by_xpath("//a[@title='{}']/../div[1]".format(shopname))
#通过actionchains 将页面拖动到该元素
action=ActionChains(driver).move_to_element(shop)
#actionchains拖动到的元素在页面的最底端,该购物网站 结算的弹层完美的挡住了该元素,还需要将页面继续
#往下拉,但是在实际测试中发现,使用Actionchains拖动元素后,整个页面都僵住了,直接输入
#action.send_keys(Keys.PAGE_DOWN).perform()是无效的,因此在前面添加了
#ActionChains(driver).click()
ActionChains(driver).click()
action.send_keys(Keys.PAGE_DOWN).perform()
#最后重新定位商品再点击
ActionChains(driver).click()
time.sleep(2)
shop = driver.find_element_by_xpath("//a[@title='{}']/../div[1]".format(shopname))
shop.click()