Home > Mobile >  Click is not going through on website selenium python
Click is not going through on website selenium python

Time:08-24

I am trying to write a script that will automatically open to a specific bible verse with it's commentaries open, however, click does not activate the commentary window to open. What is wrong?

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

driver = webdriver.Firefox()
driver.get("https://catenabible.com/mt/1")
assert "Catena" in driver.title
elem = driver.find_element(By.ID, "mt001001")
elem.click()

CodePudding user response:

You are missing a wait.
You should wait for the page to complete loading before clicking that element.
The best way to do that is to use the Expected Conditions explicit waits.
The following code should work better:

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

driver = webdriver.Firefox()
wait = WebDriverWait(driver, 20)
driver.get("https://catenabible.com/mt/1")
assert "Catena" in driver.title
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, "#mt001001 .bibleVerseText"))).click()

CodePudding user response:

The element you want to click on is actually not the one with ID=mt001001, but the a-tag within it.

enter image description here

You can adress this element by using Xpath:

elem = driver.find_element(By.XPATH, "//a[@href='/mt/1/1']")

However, this will cause an error because it is not clickable. You could try somethin like this:

driver.execute_script("arguments[0].click();", elem)

This will not get the exact same result as when manually click on the link. Most likely there is a javascript that runs some animations that are not triggered by doing it this way.

  • Related