尝试使用python用硒单击button type =“ button”

时间:2019-10-14 22:56:27

标签: html python-3.x selenium

我想单击https://www.airbnb.com/rooms/13921817?location=Seoul%2C%20South%20Korea&previous_page_section_name=1000中的按钮。我尝试了几个代码,但失败了。

HTML代码:

<li data-id="page-2" class="_1eqazlr">
 <button type="button" class="_1ip5u88" aria-label="Page 2" aria-busy="false">
  <div class="_1bdke5s">2</div>
 </button></li>

这是我尝试过的代码:

add="https://www.airbnb.com/rooms/13921817?location=Seoul%2C%20South%20Korea&amp;previous_page_section_name=1000"

driver = webdriver.Chrome(executable_path="C:\Drivers\chromedriver_win32\chromedriver.exe")

driver.get(add)

path="//*[@id='reviews']/div/div/div/section/div[2]/div[3]/div/div/div/nav/span/div/ul/li[2]//button"

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,path))).click()

除了上面的“路径”,我还尝试了以下几种不同的XPATH:

path="//button[contains(@class,\"_lip5u88\") and contains(@aria-label,\"Page 2\")]"

path="//*[@id='reviews']/div/div/div/section/div[2]/div[3]/div/div/div/nav/span/div/ul/li[2]//button"

path="//button[@type='button']//following::div[text()='2']"

path='.//button[[@type="button" and normalize-space()="2"]'

出现以下错误:

selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button type="button" class="_1ip5u88" aria-label="Page 2" aria-busy="false">...</button> is not clickable at point (214, 871). Other element would receive the click: <div class="_10ejfg4u">...</div>
  (Session info: chrome=77.0.3865.90)

or 

selenium.common.exceptions.TimeoutException: Message: 

您能帮我弄清楚吗?非常感谢你!

1 个答案:

答案 0 :(得分:0)

这应该有效:

driver.execute_script("""
  document.querySelector('[aria-label="Page 2"]').click()
""")