Reputation: 47
I'm very new to Python and using selenium for the first time. What I need:
What I tried: I tried clicking on the button using selenium and it reloaded the same page even though the url showed the next page. my code:
driver.get("http//www.kegg.jp/kegg-bin/get_htext?ban00001.keg")
mouse = webdriver.ActionChains(driver) elem=driver.find_element_by_xpath("//img[@src='/Fig/get_htext/open.png']")
mouse.move_to_element(elem)
driver.implicitly_wait("5")
mouse.click().perform()
I also tried to avoid clicking and directly fill the form that the button submits but it seems I'm filling the wrong xpath as it cannot find the element.
elem=driver.find_element_by_xpath("/body/form/input[@name='htext']").send_keys('ban00001.keg')
Can somebody tell me why the click did not work and how is my xpath wrong? But mainly, how can I accomplish my goal of opening the page. Please help. This is worth 10 college credits and I'm awfully behind on my project because of this!
Upvotes: 2
Views: 1758
Reputation: 474003
First of all, there are 4 almost identical arrows. Since you need the 4th one, I would use the alt
attribute to distinguish the arrows:
driver.find_element_by_xpath("//img[@alt='4th Level']")
When you click the arrow, it takes time for the page to load - you need to wait for it. I'd wait for the main page grid to become visible via the visibility_of_element_located
expected condition.
Complete working code:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
driver = webdriver.Chrome()
driver.get("http://www.kegg.jp/kegg-bin/get_htext?ban00001.keg")
# locating the 4th arrow
arrow4 = driver.find_element_by_xpath("//img[@alt='4th Level']")
arrow4.click()
# wait for the grid to become visible
wait = WebDriverWait(driver, 10)
wait.until(expected_conditions.visibility_of_element_located((By.ID, "grid")))
print(driver.page_source)
driver.quit()
Upvotes: 1