Reputation: 1032
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:
I get the following code while inspecting the source code of this arrow:
<div id="next" class="left"><a href="#" title="next"> </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
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
Reputation: 11
you can use below css selectors:
.left [title='Next']
div.left
div .left [title='Next']
Upvotes: 1
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