硒:点击按钮

2024-06-28 15:49:30 发布

您现在位置:Python中文网/ 问答频道 /正文

它是页面的html-代码的一部分,我应该在这里单击按钮

<div class="add-company-form__form-control add-company-form__submit">
    <button class="button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem" data-bem='{"button":{}}' role="button" type="submit">
        <span class="button__text">Добавить организацию</span>
    </button>
</div>

我试着用手指按一下按钮

driver.find_element_by_xpath("//div[@class='add-company-form__submit']/button").click()

但它又回来了

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@class='add-company-form__submit']/button"}

我该怎么修


Tags: divformaddtypebutton页面element按钮
2条回答

您应该在单击按钮之前尝试实例化WebDriver等待:

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.XPATH, "//button[@class='button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem']"));

注:

我使用了Java语法,但在Python中应该非常相似

尝试使用Xpath

  WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='button button_theme_islands button_size_xl button_view_action button_type_submit button__control i-bem']")))

如果你想使用CssSelector,试试这个

   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.button_theme_islands")))

相关问题 更多 >