尝试使用python使用硒单击按钮类型=“按钮”

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

我想单击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()

除了上面的“ path”,我还尝试了如下几种不同的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: 

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

html python-3.x selenium
2个回答
0
投票

这应该起作用:

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

0
投票

首先尝试滚动到last review

path="//button[@aria-label='Page 2']"
element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,path)))
last_review = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH,"(//div[@data-review-id])[last()]")))
driver.execute_script("arguments[0].scrollIntoView(true);", last_review)
element.click()
© www.soinside.com 2019 - 2024. All rights reserved.