社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Moshe Slavin johnnybigH  »  全部回复
回复总数  2
6 年前
回复了 Moshe Slavin johnnybigH 创建的主题 » 廉价航班项目/按钮元素返回“NoneType”对象错误Selenium python

有效的方法是:

self.driver.execute_script("return 
document.getElementsByClassName('flight-select__flight-date-picker__button 
flight-select__flight-date-picker__button--next')[0].click();")
6 年前
回复了 Moshe Slavin johnnybigH 创建的主题 » 廉价航班项目/按钮元素返回“NoneType”对象错误Selenium python

你的方法 find 始终不返回,因此出现错误 'NoneType' object :

def find(self, by, value):
    element = WebDriverWait(
        self.driver, 20).until(
        EC.visibility_of_element_located((by, value)))
    self.web_element = element
    return None     

你应该做的是返回元素。。。

def find(self, by, value):
    element = WebDriverWait(
        self.driver, 20).until(
        EC.visibility_of_element_located((by, value)))
    self.web_element = element
    return self.web_element # or return element

另外,将XPath更改为更健壮的使用:

# Button "Next"
self.click(By.XPATH, '//button[@class="flight-select__flight-date-picker__button flight-select__flight-date-picker__button--next"]')

编辑:

你可以用 ActionChains 要使用偏移单击:

button_element = self.find(By.XPATH, '//button[@class="flight-select__flight-date-picker__button flight-select__flight-date-picker__button--next"]')
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(button_element, 5, 5).click().perform()

希望这对你有帮助!