2014-02-27 22 views
0

saksoff5th.com是我正在使用的网站URL。我通过主页上的文字“腰带”进行搜索,然后单击男士链接。这将我带到男士腰带的产品阵列页面。我想单击阵列上的第二个产品,但是出现错误。硒蟒蛇 - 产品阵列页面,如何动态点击产品

我使用的代码是。在这里,我试图把所有这些放在一个列表中,然后单击页面上的第二个产品。

elemprodcl = browser.find_element_by_id('search-result-items') 
Listprdcl= elemprodcl.find_elements_by_class_name('grid-tile') 
elemprodcl2 = Listprdcl[1].find_element_by_class_name('product-tile') 
elemprodcl3 = elemprodcl2.find_element_by_class_name('product-image') 
elemprodcl4 = elemprodcl3.find_element_by_tag_name('a').click() 

错误所收到: -

Traceback (most recent call last): 
    File "C:\Python27\Off5th_Guest_Checkout", line 39, in <module> 
    elemprodcl4 = elemprodcl3.find_element_by_tag_name('a').click() 
    File "C:\Python27\lib\selenium\webdriver\remote\webelement.py", line 59, in click 
    self._execute(Command.CLICK_ELEMENT) 
    File "C:\Python27\lib\selenium\webdriver\remote\webelement.py", line 369, in _execute 
    return self._parent.execute(command, params) 
    File "C:\Python27\lib\selenium\webdriver\remote\webdriver.py", line 164, in execute 
    self.error_handler.check_response(response) 
    File "C:\Python27\lib\selenium\webdriver\remote\errorhandler.py", line 164, in check_response 
    raise exception_class(message, screen, stacktrace) 
ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: 

回答

0

那么你几乎没有。您试图点击a Web元素,因为您应该尝试点击img

所以从上次中断的地方继续:

elemprodcl4 = elemprodcl3.find_element_by_tag_name('a') 
elemprodcl4.find_element_by_tag_name('img').click() 

或者简单地通过使用一个CSS选择器:

browser.find_element_by_css_selector('.search-result-items li:nth-child(2) div > div > a').click() 

的CSS选择器的说明syntax-

  • .search-result-items是作为文本的类名前面有一个 点。
  • li是上述课程的后代(孩子)。和nth-child 指示哪个孩子从列表中(阵)的孩子做我们要 选择
  • 类似的diva,他们是前 选择的孩子提到
+0

谢谢Amey,的点击图像完成了这项工作。不过,我想尝试通过CSS选择器。我不太清楚如何使用它。你能解释一下语法和我们如何使用它吗?会真的很感激它。 – user3337644

+0

编辑我的答案 – Amey