【问题标题】:"Non html button" not clicable using seleinum使用 selenium 无法点击“非 html 按钮”
【发布时间】:2019-10-09 01:46:31
【问题描述】:

我正在尝试使用 python 和 selenium 来单击一个元素而没有成功,就像我想单击按钮元素时一样。

python --version
Python 2.7.16

print selenium.__version__
3.141.0

chromedriver --version
ChromeDriver 2.36

chromium-browser --version
Chromium 65.0.3325.181

这是标签:

<a data-fblog="the_button" href="javascript:" id="the_btn" class="the_btn" title="Goto">Goto</a>

(显然没有定义为按钮)

这是负责点击该标签的python部分:

chrome_options = Options()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome('PATHTOCRHROMEDRIVER/chromedriver',options=chrome_options)
driver.get('https://www.urltogetinfo.com')

try:
    the_button = driver.find_element_by_id('the_btn')
    the_button.click()
    #Also tried these aproaches without sucess:
    #the_button = driver.find_element_by_id('the_btn').send_keys(Keys.RETURN)
    #the_button = driver.find_element_by_id('the_btn').send_keys(Keys.ENTER)
    sleep(5)
except:
    print("Problem clicking the button")
    #would like to log the exception here
    pass

我做错了什么?

【问题讨论】:

  • 您是否尝试添加等待可点击或验证元素不在IFRAME 内?
  • 你检查过这个按钮是否存在于 iframe 中吗?

标签: python selenium


【解决方案1】:

尝试以下选项

  1. 尝试在 webdriver 初始化之后或单击元素之前添加隐式等待。 driver.implicitly_wait(15)
  2. 尝试添加显式等待按钮

WebDriverWait(driver, 3).until(EC.presence_of_element_located((By.ID, "the_btn")))

  1. 检查按钮是否存在于 iframe 中。如果存在于 iframe 标记中,请在单击按钮之前切换到 iframe

【讨论】:

  • driver.implicitly_wait(15) 工作正常!需要谷歌并了解这是如何解决的以及其他解决方案也是如何解决的。谢谢。
  • 混合driver.implicitly_wait(15) 并调用WebDriverWait 不是很好的做法。这是混合隐式和显式等待的示例,自动化中不建议这样做,因为您可能会遇到意外的等待时间。使用其中一个是可以的,但两者一起使用是不可取的。 stackoverflow.com/questions/29474296/…
【解决方案2】:

您可以尝试在元素存在时调用 WebDriverWait,然后在此处使用 Javascript 进行单击。

the_button = WebDriverWait(driver, 15).until(EC.presence_of_element_located((By.ID, "the_btn")))
driver.execute_script("arguments[0].click();", the_button)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 2019-05-30
    相关资源
    最近更新 更多