Home > Enterprise >  Selenium located the element but it fails to click on it
Selenium located the element but it fails to click on it

Time:03-23

So I was trying to get the mp4 url from the video url. I tried to locate the div and click on it but it failed to even locate it. So I tried locating the svg inside it and located it successfully but whenever I try to click on it try to get the requests made by the website I don't get the requests made and the element does not get clicked. Whenever I try not to click it I get the requests made but when I try to click I only get errors and stack trace-

The Code

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from seleniumwire import webdriver  # Import from
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
import requests
from bs4 import BeautifulSoup

#Chrome Stuff
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')

#All the driver Stuff
driver = webdriver.Chrome(options=chrome_options)
# driver.get("https://youtube.com")
url = 'https://fembed-hd.com/v/l364xun6eeqrx67'
driver.get(url)
# driver.implicitly_wait(0.5)

# Clicking...
body = driver.find_element(by=By.TAG_NAME, value='body')
body.click()
parent = driver.window_handles[0]
driver.switch_to.window(parent)

body.click()
parent = driver.window_handles[0]
driver.switch_to.window(parent)

# BTN Stuff
driver.implicitly_wait(5)
btnplay = driver.find_element(By.TAG_NAME, "svg")
print(btnplay)
btnplay.click()

driver.implicitly_wait(2)
# Access requests via the `requests` attribute
for request in driver.requests:
    if request.response:
        print(request.url)
driver.quit()
driver.close()

The console with click method -

<selenium.webdriver.remote.webelement.WebElement (session="fb0fadcbd64dc589211b0f3ef7052618", element="0f996a92-3f40-497e-a7f1-ff7dedb5e945")>
Traceback (most recent call last):
  File "main.py", line 38, in <module>
    btnplay.click()
  File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 81, in click
    self._execute(Command.CLICK_ELEMENT)
  File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webelement.py", line 710, in _execute
    return self._parent.execute(command, params)
  File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 425, in execute
    self.error_handler.check_response(response)
  File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: headless chrome=97.0.4692.71)
Stacktrace:
#0 0x55691c752f23 <unknown>
#1 0x55691c21d6cf <unknown>
#2 0x55691c254a27 <unknown>
#3 0x55691c248899 <unknown>
#4 0x55691c271182 <unknown>
#5 0x55691c248163 <unknown>
#6 0x55691c27128e <unknown>
#7 0x55691c2843db <unknown>
#8 0x55691c271073 <unknown>
#9 0x55691c246bca <unknown>
#10 0x55691c247c95 <unknown>
#11 0x55691c78273d <unknown>
#12 0x55691c798a40 <unknown>
#13 0x55691c784435 <unknown>
#14 0x55691c799465 <unknown>
#15 0x55691c777e6f <unknown>
#16 0x55691c7b42d8 <unknown>
#17 0x55691c7b4458 <unknown>
#18 0x55691c7cf1fd <unknown>
#19 0x7f779ae0cd40 <unknown>

As you can see I don't even get the requests made. I need the requests made after clicking the svg play button. Please help Me. I will be grateful and glad for hearing the answers.

CodePudding user response:

Well, after clicking on some element there and getting back to the original page I could click on svg element to start play the video.
The issues here are: To click on correct element and to locate elements with special tag names like svg.
Once the ad is opened we can play the video. It can be done not by clicking the svg (picture) element only.
So, my code is:

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

driver = webdriver.Chrome()
driver.maximize_window()
url = 'https://fembed-hd.com/v/l364xun6eeqrx67'
driver.get(url)
wait = WebDriverWait(driver, 20)

wait.until(EC.presence_of_element_located((By.XPATH, "//div[contains(@style,'fixed')]"))).click()

parent = driver.window_handles[0]
driver.switch_to.window(parent)

driver.find_element(By.XPATH, "//*[name()='svg']").click()
  • Related