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制作一个简单的机器人,将产品添加到购物车中。我正在使用硒这样做。我试着让硒点击“添加到包里”。按钮,但是当我运行时我得到这个错误:
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"Add to Bag"}
它无法找到'添加到包'网站上的按钮,即使我知道它在那里。我做错了什么?
提前致谢
答案 0 :(得分:0)
有时候我也会遇到同样的问题。通过文本获取元素。这是通过XPath获取元素的最好方法。希望这个XPath可以解决你的问题
<itemHyperlink hyperlinkType="Reference">
<hyperlinkReferenceExpression><![CDATA["./dashboard/viewer.html#%2Fpublic%2FP2%2FMidcap%2FFinancial%2FDashboards%2FWell_Profile"+"&hidden_WellConcatenated_0=" + $V{WellConcatenated_0} + "&hidden_OccurrenceDate_1=" + $P{RecordDate_0_1} + "&hidden_OccurrenceDate_2=" + $P{TimeStampMinusOneWeek}]]></hyperlinkReferenceExpression>
</itemHyperlink>
希望通过XPath获取元素将解决您的问题
答案 1 :(得分:0)
您需要为方法提供元素的id
属性而不是文本。看看页面和文档似乎
selenium.click('add-to-bag')
应该让你到那儿。