【问题标题】:How to click on a pictorial button in web browser page using python with selenium如何使用带有 selenium 的 python 在 Web 浏览器页面中单击图片按钮
【发布时间】:2020-01-07 07:22:52
【问题描述】:
<li id="tb_7" class="siebui-toolbar-enable" data-cmd="#15" data-subtoolbar="N" role="menuitem" 
title="Site Map" name="SiteMap" tabindex="-1" data-tbindex="7" data-autom="find:[ot]"><span 
class="siebui-icon-tb-sitemap ToolbarButtonOn"><img src="images/icon_sitemap_1.gif"><span 
class="siebui-toolbar-text">Site Map</span></span></li>

我尝试使用以下代码,成功登录后让我进入应用程序但出现错误(底部):

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

username = 'abc'
password = '****'
url = 'someurl.com'

driver = webdriver.Chrome('C:/chromedriver.exe')
driver.get(url)
driver.find_element_by_name('UserName').send_keys(username)
driver.find_element_by_name('Password').send_keys(password)
driver.find_element_by_id('s_22').click()
wait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Site 
Map']"))).click()'''

错误:

File "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

【问题讨论】:

  • 您的 XPath 选择器中是否有正确的文本 - “站点地图”?当我从您的代码中复制时,我得到了 2 个空格而不是 1 个空格。
  • 能否提供剩余的错误信息?
  • @Raymond 完整错误信息:C:\Users\AppData\Local\Programs\Python\Python36-32\python.exe C:/Users/Desktop/Others/Practise/trail.py Traceback (最近一次通话最后):文件“C:/Users/Desktop/Others/Practise/trail.py”,第 15 行,在 等待(驱动程序,30).until(EC.element_to_be_clickable((By.XPATH,“ //span[text()='Site Map']"))).click() 文件 "C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\ support\wait.py",第 80 行,直到 raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

你很亲密。您可以为element_to_be_clickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.siebui-toolbar-enable[name='SiteMap'][title='Site Map']>span.siebui-icon-tb-sitemap.ToolbarButtonOn>img[src*='images/icon_sitemap_1']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='siebui-toolbar-enable' and @name='SiteMap'][@title='Site Map']/span[@class='siebui-icon-tb-sitemap ToolbarButtonOn']/img[contains(@src, 'images/icon_sitemap_1')]"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

  • 嗨 DebanjanB 我已经尝试过,但没有运气失败并出现同样的错误:(
  • 错误:文件“C:\Users\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py”,第 80 行,在直到引发 TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
猜你喜欢
  • 2020-07-31
  • 1970-01-01
  • 2012-08-24
  • 2020-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2019-10-02
相关资源
最近更新 更多