selenium的持续问题解决

问题:xpath定位不到

解决:尝试使用xpath相对路径

问题:读取中文字符报错 UnicodeDecodeError: 'utf8' codec can't decode byte 0xe5 in position 0: unexp

解决:强制转码 conf.g1.decode('UTF-8')

问题:could not be scrolled into view

解决:需要等待页面加载完成

问题:等待搜索结果,并点击第一个节果的图片,希望进入结果的详情页。但报异常,异常中的URL是首页的图片。

解决:因为使用的是相对URL,当前页面还未跳转到搜索结果页面,因此定位到的是首页的图片。

问题:搜索框只有鼠标点击后才显示’搜索‘按钮,selenium直接输入不显示。

解决:

问题:页面加载时间过长,selenium长时间等待,直到timeout. 实际并不需要完全加载所有数据才能继续操作。

解决:1. 设定drvier的timeout时间 driver.set_page_load_timeout(15)

  2. 捕获selenium的超时事件 from selenium.common.exceptions import TimeoutException, 执行 driver.execute_script('window.stop()')停止加载

问题:clear输入框的内容不成功

解决:在clear前先click一下输入框,确认鼠标定位到元素再执行clear

问题:xpath查找中添加多个条件

解决:find_element_by_xpath('//span[contains(@class, tab-label) and contains(text(), "购物车")]')

问题:右键点击

解决:from selenium.webdriver.common.action_chains import ActionChains

a = self.instance.bo.driver.find_element_by_xpath('//div[@class="suit-head"]/div')

ActionChains(self.instance.bo.driver).context_click(a).perform()

问题:

原文地址:https://www.cnblogs.com/tlmn2008/p/9306557.html