Python Selenium DOM Click事件无法按预期工作

时间:2019-03-11 05:39:13

标签: python-3.x selenium dom

我正在尝试单击分页链接(下一个按钮)。但是,单击将转到网站主页。我按类定位元素。有什么问题吗?

driver.get('https://www.marinetraffic.com/en/data/?asset_type=vessels&columns=flag,shipname,photo,recognized_next_port,reported_eta,reported_destination,current_port,imo,ship_type,show_on_live_map,time_of_latest_position,lat_of_latest_position,lon_of_latest_position&current_port_in|begins|FUJAIRAH%20ANCH|current_port_in=20585')

# Wait 30 seconds for page to load
timeout = 30
try:
    WebDriverWait(driver, timeout).until(EC.presence_of_element_located((By.CLASS_NAME, 'MuiButtonBase-root-60')))
    element = driver.find_element_by_class_name('MuiButtonBase-root-60')
    driver.execute_script("arguments[0].click();", element)


except TimeoutException:
    print("Timed out waiting for page to load")


driver.quit()

1 个答案:

答案 0 :(得分:1)

此类有33个元素,find_element_by_class_name返回第一个元素(位于标头中)。您可以使用页脚作为起点来缩小选项范围,并使用索引选择第二个按钮(当两个按钮均可用时,下一个和上一个没有区别)

element = WebDriverWait(driver, timeout).until(EC.visibility_of_element_located((By.CSS_SELECTOR, '.r-mtGridFooter-302 button:nth-of-type(2)')))
element.click()