Shubhra Agrawal
Shubhra Agrawal

Reputation: 47

Python selenium- Clicking a javascript link doesn't load the new page but shows the url

I'm very new to Python and using selenium for the first time. What I need:

  1. open http://www.kegg.jp/kegg-bin/get_htext?ban00001.keg
  2. click on the 4th downward arrow at the top the one next to the check-box named "one-click mode"
  3. get the html source for that page

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

Answers (1)

alecxe
alecxe

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

Related Questions