【问题标题】:I can´t press the button in Selenium with Python我不能用 Python 按下 Selenium 中的按钮
【发布时间】:2021-04-15 00:39:10
【问题描述】:

填写表格后,该表格位于我必须访问的框架内。在同一框架内有一个提交按钮,但我无法按下按钮。

这是按钮(Calcular precio): screenshot of the button

我已尝试将按钮按入和按出框架。在框架外它说它找不到它,当它在框架内时它不会给出错误,但它不会执行。

driver.get("https://www.asefasalud.es/")
time.sleep(2) 

#cerrar popUp:
driver.find_element_by_class_name("sj-close-popup").click() 

#form:
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "[name='calcular-seguro-medico']"))
driver.find_element_by_id("inputDiafnac1").send_keys("02") 
driver.find_element_by_id("inputMesfnac1").send_keys("02")
driver.find_element_by_id("inputAnniofnac1").send_keys("1990") 
driver.find_element_by_id("cPostalBasico").send_keys("41011")

#button:
#time.sleep(2)
driver.find_element_by_xpath("//a[@id='botonValidar']").click()
driver.switch_to.default_content()

我也尝试过使用 ActionChains 并使用 send_keys(Keys.ENTER) - send_keys("\n"),而不是 click()。使用 ActionChains,它的工作方式与没有它的情况相同。使用 send_keys 我得到以下错误:error

我不知道会发生什么。

【问题讨论】:

  • 我尝试了您的代码块,但没有发现问题。它可以直接运行,没有任何错误,即使是你提到的那个
  • 不要使用 time.sleep 等待,而是使用 WebDriverWait。像这样使用它将等待 200 秒或直到元素可交互 from selenium.webdriver.support import expected_conditions as ec from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By waiting_time = 200 # 200 秒 WebDriverWait(driver, waiting_time).until(ec.visibility_of_element_located((By.XPATH, xpath)))
  • 页面截图可以,HTML截图不行。我们不能在屏幕截图上尝试定位器等,我们可以将 HTML 作为文本。此外,将错误文本复制/粘贴到您的问题中,而不是屏幕截图。
  • @KumarShivamRay。当按下按钮的代码在框架内时,它不会出错,但不会执行。我也使用了 WebDriverWait,但结果相同。
  • @JeffC。实际上,当我尝试其他方式让它运行时会出现错误。使用该代码不会出现错误,但在 firefox WebDriver 中它不会运行。

标签: python html selenium selenium-webdriver


【解决方案1】:

我通过在框架内使用 execute_script 解决了它:

driver.execute_script("arguments[0].click();", driver.find_element_by_id("botonValidar"))

【讨论】:

    【解决方案2】:

    请将 botonValidarxpath 更改为

    driver.find_element_by_xpath("//*[@id=\"botonValidar\"]/parent::div").click()
    

    您的代码运行良好。如果解决了您的问题,请标记为答案。

    【讨论】:

    • 我已经试过了,用 id,但它仍然无法运行。
    猜你喜欢
    • 2021-02-14
    • 2022-06-30
    • 1970-01-01
    • 2010-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多