2017-10-13 81 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') 

應該讓你那裏。