当textContext包含前导和尾随空格字符时,如何单击按钮? [英] How to click on the button when the textContext contains leading and trailing white-space characters?

查看:101
本文介绍了当textContext包含前导和尾随空格字符时,如何单击按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Selenium与python一起单击以下按钮:

I'm trying to click on the following button using Selenium with python:

<button type="submit" tabindex="4" id="sbmt" name="_eventId_proceed">
          Einloggen
</button>

这只是一个简单的按钮,看起来像这样:

This is just a simple button which looks like this:

代码:

driver.find_element_by_id('sbmt').click()

这将导致以下异常:

selenium.common.exceptions.ElementNotInteractableException: Message:
Element <button id="sbmt" name="_eventId_proceed" type="submit">
could not be scrolledinto view

因此,我尝试在单击按钮之前使用ActionChains(driver).move_to_element(driver.find_elements_by_id('sbmt')[1]).perform()滚动到元素.

So, I tried scrolling to the element using ActionChains(driver).move_to_element(driver.find_elements_by_id('sbmt')[1]).perform() before clicking the button.

(使用[1]访问第二个元素,因为第一个元素会导致selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined异常.)

(Accessing the second element with [1] because the first would result in selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined exception.).

然后我用

wait = WebDriverWait(driver, 5)
submit_btn = wait.until(EC.element_to_be_clickable((By.ID, 'sbmt')))

以便等待按钮可单击.这些都没有帮助.

in order to wait for the button to be clickable. None of this helped.

我还使用了driver.find_element_by_xpath及其它,我在Firefox和Chrome上进行了测试.

I also used driver.find_element_by_xpath and others, I tested it with Firefox and Chrome.

如何在没有异常的情况下单击按钮?

How can I click on the button without getting an exception?

任何帮助将不胜感激

推荐答案

要在元素上调用click(),您需要首先将 WebDriverWait expected_conditions一起使用,以使元素成为clickable ,您可以使用以下解决方案:

To invoke click() on the element you need to first use WebDriverWait with expected_conditions for the element to be clickable and you can use the following solution:

  • 使用XPATH:

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='sbmt' and normalize-space()='Einloggen']"))).click()

  • 注意:您必须添加以下导入:

  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

  • 这篇关于当textContext包含前导和尾随空格字符时,如何单击按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆