Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
329 views
in Technique[技术] by (71.8m points)

python - Why is Selenium unable to click a button from a list of buttons?

I am trying to access a specific button on a webpage so that I can click it and webscrape what's on the other side. I'm trying to use Selenium to return a list of buttons under a specific div so that I can click the first one. However, I receive an error upon clicking the 0th index.

I receive the following error:

ElementNotInteractableException: Message: element not interactable
(Session info: chrome=87.0.4280.88)

Am I unable to click elements if they are in a list? Or could this be something else?

Webpage: https://profiles.ucr.edu/app/home

Picture of HTML and desired ID element. I want to click the red "Browse" button: div and button HTML

Here is the code that I am using, but it returns an error. I commented out the ability to print out the list of buttons:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys    
import time


PATH = "C:Program Files (x86)chromedriver.exe"    #path to webdriver executable
wd = webdriver.Chrome(PATH)

urlDepartments = 'https://profiles.ucr.edu/app/home'

wd.get(urlDepartments)
time.sleep(1)

idFirst = 'cdk-accordion-child-'
idNum = 0
idNumStr = str(0)
div = wd.find_element_by_id(idFirst + idNumStr)
button = div.find_elements_by_tag_name('button')
button[0].click()
#print(button)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to wait for the button to become clickable or maybe it is not shown on the screen yet, you can try something like this:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys    
import time


PATH = "C:Program Files (x86)chromedriver.exe"    #path to webdriver executable
wd = webdriver.Chrome(PATH)

urlDepartments = 'https://profiles.ucr.edu/app/home'

wd.get(urlDepartments)
time.sleep(1)

idFirst = 'cdk-accordion-child-'
idNum = 0
idNumStr = str(0)
div = wd.find_element_by_id(idFirst + idNumStr)
button = div.find_elements_by_tag_name('button')
print("Is visible: " + str(element_name.is_displayed()))
time.sleep(3)
button[0].click()
#print(button)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...