Selenium-python2.7:单击模式表复选框(在Selenium IDE之后)

时间:2018-10-04 17:47:49

标签: python selenium selenium-chromedriver selenium-ide

我认为我的问题与这篇文章有某种关系 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代码外观的屏幕截图,因为我无法发布真实网址enter image description here

1 个答案:

答案 0 :(得分:1)

我假设xpath字符串引起了问题。如果我的假设是正确的,则应该可以进行以下操作:

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