如何选择包含以下HTML的按钮:
<a href="#" class="js-buy ncss-brand ta-sm-c u-uppercase pt3-sm pr5-sm pb3-sm pl5-sm pt2-lg pb2-lg d-sm-b d-lg-ib test-buyable ncss-btn bg-black text-color-white">Kaufen 210,00 €</a>
以下是我要点击黑色'Kaufen'按钮。我尝试过以下方法:
buy = ui.WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="j_c29"]/div[1]/a')))
buy.click()
我收到错误:
File "/Users/xxx/test.py", line 101, in <module>
obj.run()
File "/Users/xxx/test.py", line 66, in run
buy = ui.WebDriverWait(self.driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[@id="j_c29"]/div[1]/a')))
File "/Users/xxx/Library/Python/2.7/lib/python/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
根据您在click()
上分享的元素,您需要将WebDriverWait和expected_conditions
子句作为element_to_be_clickable而不是presence_of_element_located
来引导,您可以使用以下任一选项:
WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Kaufen"))).click()
WebDriverWait(self.driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='js-buy ncss-brand ta-sm-c u-uppercase pt3-sm pr5-sm pb3-sm pl5-sm pt2-lg pb2-lg d-sm-b d-lg-ib test-buyable ncss-btn bg-black text-color-white' and contains(.,'Kaufen')]"))).click()
使用此代码:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[@id='j_c29']/div[1]/a"))).click()
如果要删除“Deine Cookie-Einstellungen”弹出窗口,请在上面的代码之前使用此代码:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div[3]/div/div/div/div/div/div/div/div/div/div/div/div/div[3]/div[2]/div"))).click()