私信  •  关注

Moshe Slavin johnnybigH

Moshe Slavin johnnybigH 最近创建的主题
Moshe Slavin johnnybigH 最近回复了
4 年前
回复了 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();")
4 年前
回复了 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()

希望这对你有帮助!