Yanirmr
Yanirmr

Reputation: 1032

How to click on a button with Selenium

I try to catch the "next" button in a viewer with Selenium in Python, but nothing works and I get an error message.

The code I executed:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=options, executable_path=r'chromedriver.exe')
driver.get('https://web.nli.org.il/sites/NLI/Hebrew/digitallibrary/pages/viewer.aspx?&presentorid=MANUSCRIPTS&docid=PNX_MANUSCRIPTS990000907570205171-1')
WebDriverWait(driver, 10)

Until now, everything has been fine. This is where the issue arises.

I want to click on the next arrow: enter image description here

I get the following code while inspecting the source code of this arrow: <div id="next" class="left"><a href="#" title="next">&nbsp;</a></div>

So these are my attempts:

XPath

driver.find_elements_by_xpath('//*[@id="next"]')

returns an empty list.

id

driver.find_elements_by_id("next")

returns an empty list.

So, how can I catch this button and click on it?

Upvotes: 1

Views: 85

Answers (3)

YaDav MaNish
YaDav MaNish

Reputation: 1352

//div[@id='next']//child::a there could be different element with the id next so specifically define for the div and, I can see the a is the child of div

you can check it with the explicitWait

wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@id='next']//child::a"))).click()

Also found an iframe with the id = MainIframe and name = zoomer under which the desired element is available, so you could switch to the frame and get the desired element

Upvotes: 1

Vivek Sahu
Vivek Sahu

Reputation: 11

you can use below css selectors:

  1. .left [title='Next']
  2. div.left
  3. div .left [title='Next']

Upvotes: 1

cruisepandey
cruisepandey

Reputation: 29382

That arrow is in iframe, so driver needs to change it's focus.

and then you can use explicit wait to click on it :

CSS_SELECTOR :

div#next

Sample code :

driver = webdriver.Chrome(options=options, executable_path=r'chromedriver.exe')
driver.get('URL here')
WebDriverWait(driver, 10)
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.ID, "MainIframe")))
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#next"))).click()

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Upvotes: 1

Related Questions