【问题标题】:Selenium-python2.7: click on a modal table checkbox (following Selenium IDE)Selenium-python2.7:单击模态表复选框(遵循 Selenium IDE)
【发布时间】:2018-10-04 17:47:48
【问题描述】:

我认为我的问题与这篇文章有关 Selenium - cannot click on an element inside a modal

我点击一个元素,它会打开一个模态表。然后,我想在一个长长的列表中选择一个特定的 checknox,但 Selenium 给了我一个错误。

这就是我所做的:

我首先使用 Selenium IDE 进行了测试:首先我记录了操作,但是当我尝试在日志中重放它们时,我得到了这个:

Running 'Step2_sele'
1.open on /reserve-space/... OK
2.click on css=a[title="Locations"] > span... OK
3.click on linkText=Add/Remove Locations... OK
4.Trying to find xpath=//input[@value='2427']... Failed:
Element is not currently visible and may not be manipulated

我认为我应该给元素更多的时间来显示,我用 Python 编写了这个

browser = webdriver.Chrome()
browser.get(url_res)
time.sleep(10)
browser.find_element_by_css_selector('a[title="Locations"]>span').click()
time.sleep(10)
browser.find_element_by_css_selector('a.dynamic-filter-item-add.summary').click()
time.sleep(10)
browser.find_element_by_xpath("xpath=//input[@value='2427']").click()

但我得到了另一个错误

Traceback (most recent call last):
  File "bot.py", line 69, in <module>
    browser.find_element_by_xpath("xpath=//input[@value='2427']").click()
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 393, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
    'value': value})['value']
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Unable to locate an element with the xpath expression xpath=//input[@value='2427'] because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document': The result is not a node set, and therefore cannot be converted to the desired type.
  (Session info: chrome=69.0.3497.100)
  (Driver info: chromedriver=2.41.578737 (49da6702b16031c40d63e5618de03a32ff6c197e),platform=Windows NT 10.0.16299 x86_64)

这是 html 代码的截图,因为我无法发布真实的 url

【问题讨论】:

  • 为什么将"xpath=" 添加到您的XPath 中?你认为它应该怎么做?
  • 我对 Selenium 和 HTML 很陌生,我想我从错误的例子中做了一些剪切和粘贴。我没有看到任何其他解释!

标签: python selenium selenium-chromedriver selenium-ide


【解决方案1】:

我认为 xpath 字符串会导致问题。如果我的假设是正确的,以下应该有效:

browser.find_element_by_xpath("//input[@value='2427']").click()

【讨论】:

    猜你喜欢
    • 2017-09-24
    • 2021-12-11
    • 2018-04-24
    • 2020-10-06
    • 1970-01-01
    • 1970-01-01
    • 2019-10-10
    • 2022-01-07
    • 1970-01-01
    相关资源
    最近更新 更多