2017-10-13 47 views
0
class PythonOrgSearch(unittest.TestCase): 

    def setUp(self): 
     self.driver = webdriver.Chrome() #opens chrome to complete the task 

    def test_search_in_python_org(self): 
     driver = self.driver 
     driver.get(URL) #uses the URL that was generated at the start of the task 
     self.assertIn("adidas", driver.title) 
     elem = driver.find_element_by_name("Add to Bag") #finds the 'add to bag' button (for adidas.com) and clicks it 
     elem.send_keys("pycon") 
     elem.send_keys(Keys.RETURN) 
     assert "No results found." not in driver.page_source 
     selenium.click("Add to Bag") 


    def tearDown(self): 
     self.driver.close() 

if __name__ == "__main__": 
    unittest.main() 

现在我正在试验python并试图为adidas.com制作一个简单的机器人,它将产品添加到购物车中。我使用硒来做到这一点。我努力硒点击“添加到购物篮”按钮,但是当我跑我得到这个错误:Python硒:无法定位网页上的元素

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Add to Bag"} 

它不能在网站上找到了“添加到购物篮”按钮,即使我知道它的存在。我究竟做错了什么?

在此先感谢

+0

您可以添加网站的网址 – Hackerman

+0

是的,这里是我的代码的顶部:高清UrlGen(型号,尺寸): BaseSize = 550 #size 5,上升10各占一半大小 ShoeSize =大小 - 5 ShoeSize = ShoeSize * 20 RawSize = ShoeSize + BaseSize ShoeSizeCode = int(RawSize) URL ='http://www.adidas.com/us/'+ str(model)+'.html?forceSelSize ='+ str(模型)+ '_' + STR(ShoeSizeCode) 返回URL 模型=的raw_input( '型号') 尺寸=输入( '尺寸:') URL = UrlGen(型号,尺寸) 打印(STR( URL) ) – cgreene

回答

0

有时我也由text.It的最佳途径获得元素面临着同样的问题,通过XPath.Hope获得元素这个XPath将解决您的问题

elem =driver.find_element_by_xpath("//*[text()='Add to Bag']") 

Hope getting element by XPath will solve your problem

0

您需要提供元素的id属性而不是文本的方法。看看页面和文档似乎

selenium.click('add-to-bag') 

应该让你那里。