'Selenium Can't locate the element while trying to click
I was scraping fembed just as a personal project but I kept running into errors.For getting the streaming url I needed to click the get the request through selenium wire. So I was trying to click on the button on this url. Whenever I try to click it says can't find element.
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)
# bs4
r = requests.get(url) # r variable has all the HTML code
htmlContent = r.content # r returns response so if we want the code we write r.content
# print(htmlContent) # printing the code
soup = BeautifulSoup(htmlContent, 'html.parser')
# print(soup.prettify()) # to print html in tree structure
# Clicking...
body = driver.find_element_by_tag_name("body")
body.click()
parent = driver.window_handles[0]
chld = driver.window_handles[1]
driver.switch_to.window(parent)
body.click()
parent = driver.window_handles[0]
chld = driver.window_handles[1]
driver.switch_to.window(parent)
# play button click
# btnplay = driver.find_element_by_xpath("//div[@class='loading-container']")
# print(btnplay)
wait = WebDriverWait(driver, 5)
try:
element = wait.until(EC.element_to_be_clickable((By.CLASS_NAME, 'loading-container')))
element.click()
print(f'Element found: {element}')
except TimeoutException:
print('could not find it.')
# btnplay.click()
# btnplaybs4 = soup.find("div", class_="loading-container")
driver.implicitly_wait(5)
# Access requests via the `requests` attribute
for request in driver.requests:
if request.response:
print(
request.url
)
driver.quit()
driver.close()
The console-
main.py:32: DeprecationWarning: find_element_by_tag_name is deprecated. Please use find_element(by=By.TAG_NAME, value=name) instead
body = driver.find_element_by_tag_name("body")
could not find it.
https://accounts.google.com/ListAccounts?gpsia=1&source=ChromiumBrowser&json=standard
https://fembed-hd.com/v/l364xun6eeqrx67
https://fembed-hd.com/asset/default/player/base.js?v=1595607720
https://fembed-hd.com/api/source/l364xun6eeqrx67
https://fembed-hd.com/asset/jquery/slim-3.2.min.js?c=US&t=470
https://fembed-hd.com/asset/userdata/376887/poster/l/36/l364xun6eeqrx67.png?v=1645401494
https://mc.yandex.ru/metrika/tag.js
https://mc.yandex.ru/watch/56313682
https://thumb.fvs.io/asset/userdata/376887/poster/l/36/l364xun6eeqrx67.png?v=1645401494
https://mc.yandex.ru/watch/56313682/1
https://mc.yandex.ru/metrika/advert.gif
https://mc.yandex.ru/watch/49788082?wmode=7&page-url=https%3A%2F%2Ffembed-hd.com%2Fv%2Fl364xun6eeqrx67&charset=utf-8&site-info=%7B%22owner%22%3A%22376887%22%7D&browser-info=pv%3A1%3Agdpr%3A14%3Avf%3A7oivoclvhnrnrlmt4hr%3Afp%3A2198%3Afu%3A0%3Aen%3Autf-8%3Ala%3Aen-US%3Av%3A760%3Acn%3A1%3Adp%3A0%3Als%3A1412482081836%3Ahid%3A689365585%3Az%3A0%3Ai%3A20220321121023%3Aet%3A1647864623%3Ac%3A1%3Arn%3A524109570%3Arqn%3A1%3Au%3A1647864623700425535%3Aw%3A772x451%3As%3A800x600x24%3Ask%3A1%3Ahdl%3A1%3Acpf%3A1%3Ans%3A1647864619770%3Ads%3A0%2C401%2C160%2C41%2C5%2C0%2C%2C615%2C1%2C%2C%2C%2C1309%3Awv%3A2%3Aco%3A0%3Arqnl%3A1%3Ast%3A1647864623%3At%3AVideo%20shingeki-no-kyojin-the-final-season-part-2-episode-71645401122.0.mp4&t=gdpr(14)aw(1)ti(2)
https://fembed-hd.com/asset/default/img/favicon.ico?v=1553817714
https://adoptexplorationanguish.com/c0/ec/e9/c0ece9150690e04c8083b2de90d71095.js
https://fembed-hd.com/asset/default/player/jwplayer.core.controls.html5.js
https://fembed-hd.com/asset/default/player/related.js
https://fembed-hd.com/asset/default/player/provider.cast.js
https://fembed-hd.com/asset/userdata/376887/player/18015_logo.png?v=1631969623
https://thumb.fvs.io/asset/userdata/376887/player/18015_logo.png?v=1631969623
https://eu.messenger-notify.digital/go/463726
https://www.gstatic.com/cv/js/sender/v1/cast_sender.js?loadCastFramework=1
https://fvs.io/redirector?token=bTdlZDlsOFdLVnY2Mzl5SkNCUTlWb2lPRzQxNk9IMzFZTFJjOE1GOG9SWW1iMFlFclVmd3p1RC9Ga1YxYSsvcWJocXZrOTBITDB3K0JtQ1ZkWG43VjVIY0xwcVplNWFHL01sRG1LVnA1eTRvZ0Njci9IOVR3akszd01tTS9OdlZCUktIN0l1UnpwSklxd3NaTUZOOEFGYStoWXN6QWJqYURiUT06bnpuWXN1em81b2R3M21RMXY1UnczQT097kVg
https://www.gstatic.com/cast/sdk/libs/sender/1.0/cast_framework.js
https://www.gstatic.com/eureka/clank/97/cast_sender.js
Traceback (most recent call last):
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/util/connection.py", line 95, in create_connection
raise err
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/util/connection.py", line 85, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 703, in urlopen
httplib_response = self._make_request(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 398, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connection.py", line 239, in request
super(HTTPConnection, self).request(method, url, body=body, headers=headers)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 1256, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 1302, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 1251, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 1011, in _send_output
self.send(msg)
File "/nix/store/p21fdyxqb3yqflpim7g8s1mymgpnqiv7-python3-3.8.12/lib/python3.8/http/client.py", line 951, in send
self.connect()
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connection.py", line 205, in connect
conn = self._new_conn()
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fbb987410a0>: Failed to establish a new connection: [Errno 111] Connection refused
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "main.py", line 62, in <module>
driver.close()
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 938, in close
self.execute(Command.CLOSE)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/webdriver.py", line 423, in execute
response = self.command_executor.execute(driver_command, params)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 333, in execute
return self._request(command_info[0], url, body=data)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/selenium/webdriver/remote/remote_connection.py", line 355, in _request
resp = self._conn.request(method, url, body=body, headers=headers)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/request.py", line 74, in request
return self.request_encode_url(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/request.py", line 96, in request_encode_url
return self.urlopen(method, url, **extra_kw)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/poolmanager.py", line 376, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 813, in urlopen
return self.urlopen(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/connectionpool.py", line 785, in urlopen
retries = retries.increment(
File "/home/runner/justforfun/venv/lib/python3.8/site-packages/urllib3/util/retry.py", line 592, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='localhost', port=56815): Max retries exceeded with url: /session/42d4358cbbf4ecbbf8468b38e955ea83/window (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7fbb987410a0>: Failed to establish a new connection: [Errno 111] Connection refused'))
Don't know why there are error.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|