简体   繁体   中英

Click button on JS page -Python

My code accesses a page, and I am trying to click on the button that says "Program" on the menu bar (between the words "Abstract" and "Awards")

There is a href in the html of the page that would direct me to this new page, however I am not able to click on it using selenium?

It gives this error - list' object has no attribute 'click'

here is my code

import time
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()

driver.get('https://www.kidney.org/spring-clinical/program')
time.sleep(2)
page_source = driver.page_source
soup = BeautifulSoup(page_source, 'html.parser')
element1 = driver.find_elements_by_xpath('/html/body/div[2]/nav/div/div[1]/div[3]/a')
element1.click()

find_elements_by_xpath() returns list of element and you can't click on list.

Change this to either find_element_by_xpath() which returns webelement

element1 = driver.find_element_by_xpath('/html/body/div[2]/nav/div/div[1]/div[3]/a')
element1.click()

Or use index

element1 = driver.find_elements_by_xpath('/html/body/div[2]/nav/div/div[1]/div[3]/a')
element1[0].click()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM