selenium python css selector can't find element - python

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
video = "YzmI5F_Yu1o"
driver.execute_script("window.open('about:blank');")
driver.switch_to_window(driver.window_handles[1])
driver.get("https://www.youtube.com/v/" + video)
title = driver.find_element_by_css_selector('.ytp-title-link.yt-uix-sessionlink')
returns
Traceback (most recent call last):
File "C:\Users\Gebruiker\Documents\playlist.py", line 36, in <module>
add_yt("YzmI5F_Yu1o")
File "C:\Users\Gebruiker\Documents\playlist.py", line 33, in add_yt
title = driver.find_element_by_css_selector('.ytp-title-link.yt-uix-sessionlink')
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 498, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 832, in find_element
'value': value})['value']
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 297, in execute
self.error_handler.check_response(response)
File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".ytp-title-link.yt-uix-sessionlink"}
(Session info: chrome=60.0.3112.101)
(Driver info: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 x86_64)
basically, it says that there is no element with the css selector .ytp-title-link.yt-uix-sessionlink, even though there clearly is.
element I am trying to return;
<a class="ytp-title-link yt-uix-sessionlink" target="_blank" data-
sessionlink="feature=player-title" href="https://www.youtube.com/watch?
v=YzmI5F_Yu1o">Marco Borsato - Mooi (official video)</a>

Try to switch to embed element just like to iframe and get required text:
driver.switch_to.frame(driver.find_element_by_xpath('//embed'))
title = driver.find_element_by_xpath('//a[#data-sessionlink="feature=player-title"]').text

Related

Super new to python, trying to fill web form and login

from selenium import webdriver
import time
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
web = webdriver.Chrome()
web.get('https://id.pcgeducation.com/id093')
time.sleep(4)
user = web.find_element(By.CLASS_NAME,"Name")
user.send_keys("aaaaaa#paaaas.com")
It is giving me the errors
Traceback (most recent call last):
File "C:\Users\Bowie\PycharmProjects\AutomatedFormFilling\main.py", line 10, in <module>
user = web.find_element(By.CLASS_NAME,"Name")
File "C:\Users\Bowie\PycharmProjects\AutomatedFormFilling\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 857, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\Bowie\PycharmProjects\AutomatedFormFilling\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 435, in execute
self.error_handler.check_response(response)
File "C:\Users\Bowie\PycharmProjects\AutomatedFormFilling\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".Name"}
(Session info: chrome=103.0.5060.134)
I really don't know what I am doing wrong with the find element class. Do I not have the correct PIP modules installed?

Python Selenium Opera for Youtube - getting error

I am trying the code from the following URL and it seems to be working for Opera-
Drive Opera with selenium python
import time
from selenium import webdriver
from selenium.webdriver.chrome import service
webdriver_service = service.Service('.\operadriver.exe')
webdriver_service.start()
driver = webdriver.Remote(webdriver_service.service_url, webdriver.DesiredCapabilities.OPERA)
driver.get('https://www.google.com/')
input_txt = driver.find_element_by_name('q')
input_txt.send_keys('operadriver\n')
time.sleep(5) #see the result
driver.quit()
But I get error when trying to search in Youtube with the following Xpath code-
driver.get('https://youtube.com/')
input_txt = driver.find_element_by_xpath('//*[#id="search"]')
input_txt.send_keys('operadriver\n')
Error
Traceback (most recent call last):
File "C:\Users\admin\Desktop\py\-test\web\selnm\ytb1.py", line 39, in <module>
input_txt.send_keys('operadriver\n')
File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 479, in send_keys
'value': keys_to_typing(value)})
File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\admin\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
(Session info: chrome=93.0.4577.82)
(Driver info: operadriver=93.0.4577.63 (ff5c0da2ec0adeaed5550e6c7e98417dac77d98a-refs/branch-heads/4577#{#1135}),platform=Windows NT 6.1.7601 SP1 x86_64)
Any suggestion why it's not working for youtube?
Try this
driver.get('https://youtube.com/')
input_txt = driver.find_element_by_xpath('//input[#id="search"]')
input_txt.click()
time.sleep(1)
input_txt.send_keys('operadriver\n')

Finding a element from a website with, using classname, css-selector and xpath

I'm looking for some help with my code.
I'm trying to print out an element from a website, but I get different error messages from the methods I try, none of them work.
I've tried using class_name with the following code:
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_class_name(poeisz_classname)
print(elem.text)
this returned the following error message:
Traceback (most recent call last):
File "/home/cena/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 9, in <module>
elem = browser.find_element_by_class_name(poeisz_classname)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 564, in find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidSelectorException: Message: invalid selector: Compound class names not permitted
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Linux 4.15.0-50-generic x86_64)
So I tried using xpath and css_selector to work around it:
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_xpath("//*[#class='" + poeisz_classname + "']")
print(elem.text)
from selenium import webdriver
browser = webdriver.Chrome('/home/cena/Downloads/chromedriver')
poeisz_url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
poeisz_classname = "full-product full-product--price__for"
browser.get(poeisz_url)
elem = browser.find_element_by_css_selector("*[class^='" + poeisz_classname + "']")
print(elem.text)
they, in order, returned these errors:
Traceback (most recent call last):
File "/home/cena/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 9, in <module>
elem = browser.find_element_by_xpath("//*[#class='" + poeisz_classname + "']")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#class='full-product full-product--price__for']"}
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Linux 4.15.0-50-generic x86_64)
Traceback (most recent call last):
File "/home/cena/.PyCharmCE2019.1/config/scratches/scratch_2.py", line 9, in <module>
elem = browser.find_element_by_css_selector("*[class^='" + poeisz_classname + "']")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"*[class^='full-product full-product--price__for']"}
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Linux 4.15.0-50-generic x86_64)
I'm quite new to programming, so I'm sorry if it's something stupid. Thanks in advance for your help!
You should use WebDriverWait:
for example XPath:
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('/home/cena/Downloads/chromedriver')
url = "https://webwinkel.poiesz-supermarkten.nl/boodschappen/producten/631258"
driver.get(url)
my_el = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//div[#id="products"]//*[#class="full-product full-product--price__for"]')))
print(my_el.text)
The output is:
0 99
The better way to use WebDriverWait is to use it as a var:
wait = WebDriverWait(driver, 20)
my_el = wait.until(EC.presence_of_element_located((By.XPATH, '//div[#id="products"]//*[#class="full-product full-product--price__for"]')))
As #DebanjanB has commented you might want to use visibility_of_element_located() not presence_of_element_located.
Hope this helps you!
The problem you are running into is explained in the first error message.
invalid selector: Compound class names not permitted
find_element_by_class_name() takes a single class name as a parameter but "full-product full-product--price__for" contains two class names (NOTE: class names are separated by spaces, so there are two here... "full-product" and "full-product--price__for".
You can instead convert your locator into a CSS selector, .full-product.full-product--price__for, and it should work just fine.
To read more about CSS selectors, see the links below.
Selenium Tips: CSS Selectors
WC3 reference, Selectors Level 4
Taming Advanced CSS Selectors

NoSuchElementException: no such element: Unable to locate element: {"method":"name","selector":"btnk"}

from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(r"C:\Users\m\Desktop\python\selen\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("https://google.com")
driver.find_element_by_name("q").send_keys("Automation")
driver.find_element_by_name("btnk").send_keys(Keys.ENTER)
time.sleep(4)
driver.quit()
I am trying this simple code to open google, search something with the above code. But I get this error message:
Traceback (most recent call last):
File "C:\Users\m\Desktop\python\selen\auto.py", line 9, in <module>
driver.find_element_by_name("btnk").send_keys(Keys.ENTER)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"btnk"}
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Windows NT 10.0.17134 x86_64)
That should be solved your problem :
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("/usr/bin/chromedriver")
driver.set_page_load_timeout(10)
driver.get("https://google.com")
search = driver.find_element_by_name('q')
search.send_keys("google search through python")
search.send_keys(Keys.RETURN)
time.sleep(4)
driver.quit()
I tested in Ubuntu + chrome browser
Replace your code
driver.find_element_by_name("q").send_keys("Automation")
driver.find_element_by_name("btnk").send_keys(Keys.ENTER)
To =>
search = driver.find_element_by_name('q')
search.send_keys("google search through python")
search.send_keys(Keys.RETURN)

Slate Element Reference Exception when handling frames

from selenium import webdriver
driver = webdriver.Chrome(executable_path=r'C:\Users\satyanarayana\Downloads\chromedriver_win32\chromedriver.exe')
driver.get("https://www.olx.in/")
frames_tag = driver.find_elements_by_tag_name("iframe")
print(len(frames_tag))
for i in range(0,len(frames_tag)):
driver.switch_to.frame(frames_tag[i])
driver.quit()
I am getting the following exception. Can someone help me figure out what's wrong with the code.
Traceback (most recent call last):
File "C:/Users/satyanarayana/PycharmProjects/untitled1/Ad.py", line 11, in <module>
driver.switch_to.frame(frames_tag[i])
File "C:\Users\satyanarayana\PycharmProjects\untitled1\venv\lib\site-packages\selenium\webdriver\remote\switch_to.py", line 89, in frame
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
File "C:\Users\satyanarayana\PycharmProjects\untitled1\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\satyanarayana\PycharmProjects\untitled1\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=70.0.3538.77)
(Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.17134 x86_64)
When you switch to <iframe> the driver context changes to the <iframe> and can recognize only the elements inside it. You need to switch out of the <iframe> after finishing with it
for i in range(0,len(frames_tag)):
driver.switch_to.frame(frames_tag[i])
driver.switch_to.default_content()

Categories

Resources