Selenium Python:选择

问题描述 投票:1回答:2

如何选择包含以下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&nbsp;€</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: 
python selenium selenium-webdriver
2个回答
-1
投票

根据您在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()
  • XPATH: 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()

0
投票

使用此代码:

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() 
© www.soinside.com 2019 - 2024. All rights reserved.