找到按钮但无法点击它

时间:2017-10-13 07:43:02

标签: python selenium

我有一个填写facebook注册的方法,应该按"创建一个帐户"按钮。它发现它找到按钮,但由于不清楚的原因无法点击它

按钮代码是:

<button type="submit" class="_6j mvm _6wk _6wl _58mi _3ma _6o _6v" name="websubmit" id="u_0_s">Create Account</button>

我的方法是:

def submit_new_account_form(self, **credentials):

    firstname = self.driver.find_element_by_css_selector(self.__first_name_field_css)
    lastname = self.driver.find_element_by_css_selector(self.__last_name_field_css)
    number_or_email = self.driver.find_element_by_css_selector(self.__mobile_number_or_email_field_css)
    newpass = self.driver.find_element_by_id(self.__new_password_field_id)
    maleradio = self.driver.find_element_by_css_selector(self.__male_radio_css)
    femaleradio = self.driver.find_element_by_css_selector(self.__female_radio_css)
    submit_button = self.driver.find_element_by_id(self.__create_account_button_id)


    if submit_button:
        print ("submit button found")

    if maleradio:
        print("maleradio found")
    if femaleradio:
        print ("femaleradio found")
    #firstname.clear()
    if credentials['first_name']:
        firstname.send_keys(credentials['first_name'])

    #lastname.clear()
    if credentials['last_name']:
        lastname.send_keys(credentials['last_name'])

    #number_or_email.clear()
    if credentials['phone_or_email']:
        number_or_email.send_keys(credentials['phone_or_email'])
        re_enter_email_field = WebDriverWait(self.driver, 10).until(
            expected_conditions.presence_of_element_located((By.CSS_SELECTOR,self.__re_enter_new_email_field_css)))
        re_enter_email = self.driver.find_element_by_css_selector(self.__re_enter_new_email_field_css).send_keys(
            credentials['phone_or_email'])

    #newpass.clear()
    if credentials['newpass']:
        newpass.send_keys(credentials['newpass'])

    if credentials['sex'] == 'male':
        maleradio.click()

    if credentials['sex'] == 'female':
        femaleradio.click()

    submit_button.click()


    if submit_button.click():
        print('submit button clicked')

每次运行脚本提交按钮时,单击都不会出现并发生错误:

selenium.common.exceptions.ElementNotVisibleException: Message: element not visible

3 个答案:

答案 0 :(得分:0)

<button type="submit" class="_6j mvm _6wk _6wl _58mi _3ma _6o _6v"
name="websubmit" id="u_0_s">Create Account</button>

从上面,我可以看到提交按钮的ID &#39; id =&#39; u_0_s &#39;。
您是否可以确认您是否正确提交了身份证明?如果没有,请通过&#39; id =&#39; u_0_s &#39;进行更正,请尝试让我知道它是否有效。

此外,检查测试用例执行时是否显示“提交”按钮。您可以添加代码以最大化浏览器窗口。

答案 1 :(得分:0)

您可以使用此xpath:"//button[contains(text(), 'Create an account')]",它将解决您的问题

答案 2 :(得分:0)

我添加了隐含等待

WebDriverWait(self.driver,10).until(expected_conditions.visibility_of_element_located((By.ID, self.__create_account_button_id)))

之后一切顺利