Home > database >  Selenium no such element exception
Selenium no such element exception

Time:09-23

Im trying to get this link

 <ul class="row">                     
    <li class="col-md-3 col-xs-12 col-sm-6 episodeLink37027" data-lang-key="1" data-link-id="37027" data-link-target="/redirect/37027" data-external-embed="false">
        <div class="generateInlinePlayer">
            <a class="watchEpisode" itemprop="url" href="/redirect/37027" target="_blank">
                <i class="icon VOE" title="Hoster VOE"></i>
                <h4>VOE</h4>
                <div class="hosterSiteVideoButton">Video öffnen</div>
                    ...

I already tried:

button = driver.find_element_by_css_selector("watchEpisode")

and

button = driver.find_element_by_class_name("a.watchEpisode")

I always get an no such element Exception. Are there other ways to get this element?

CodePudding user response:

Try this

button = driver.find_element_by_css_selector("a.watchEpisode")

or

button = driver.find_element_by_class_name("watchEpisode")

Don't forget to add a delay / wait before accessing this element
UPD
I see no iframes there, but the locator can be improved.
Try this code:

from selenium import webdriver
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
import time
 
CHROMEDRIVER = r"C:\Users\Nico\Desktop\automate_download\chromedriver.exe"
 
driver = webdriver.Chrome(executable_path=CHROMEDRIVER)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
actions = ActionChains(driver)
 
driver.get("https://anicloud.io/anime/stream/attack-on-titan")
wait.until(EC.visibility_of_element_located((By.XPATH, '//*[@title="Staffel 1 Episode 1"]'))).click()

button = wait.until(EC.presence_of_element_located((By.XPATH, "//li[not(contains(@style,'none'))]//i[@title='Hoster VOE']/..//div[@class='hosterSiteVideoButton']")))

actions.move_to_element(button).perform()
time.sleep(0.5)
#now you can click this element
button.click()

CodePudding user response:

Try this one, see if it works.

button = driver.find_element_by_xpath("//*[@class='watchEpisode']")

CodePudding user response:

probably you can try with this css selector :

div.generateInlinePlayer>a.watchEpisode

or this xpath:

//div[@class='generateInlinePlayer']//child::a[@class='watchEpisode']

PS : Please check in the dev tools (Google chrome) if we have unique entry in HTML DOM or not.

Steps to check:

Press F12 in Chrome -> go to element section -> do a CTRL F -> then paste the xpath and see, if your desired element is getting highlighted with 1/1 matching node.

to get the href you can try the below code :

Code trial 1 :

time.sleep(5)
val = driver.find_element_by_xpath("//div[@class='generateInlinePlayer']//child::a[@class='watchEpisode']").get_attribute('href')
print(val)

Code trial 2 :

val = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='generateInlinePlayer']//child::a[@class='watchEpisode']"))).get_attribute('href')
print(val)

Code trial 3 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='generateInlinePlayer']//child::a[@class='watchEpisode'])
val = driver.execute_script("return arguments[0].value", button)
print(val)

Code trial 4 :

time.sleep(5)
button = driver.find_element_by_xpath("//div[@class='generateInlinePlayer']//child::a[@class='watchEpisode']")
ActionChains(driver).move_to_element(button).perform()
print(button.get_attribute('href'))

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
  • Related