I've been trying to select the Play button on Spotify's webplayer through selenium for a series of tests, but have been unable to successfully target it. I've tried through CSS Selector:
driver.find_element_by_css_selector("#header .button-primary").click()
and xpath:
driver.find_element_by_xpath("//button[#class=\"button-primary\"]")
This is my error call stack:
Traceback (most recent call last): File "Spotify Dumbass.py", line
37, in test_spotify_dumbass
driver.find_element_by_css_selector("#header .button-primary").click() File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 396, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector) File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 684, in find_element
{'using': by, 'value': value})['value'] File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py",
line 195, in execute
self.error_handler.check_response(response) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py",
line 170, in check_response
raise exception_class(message, screen, stacktrace) NoSuchElementException: Message: Unable to locate element:
{"method":"css selector","selector":"#header .button-primary"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/fxdriver#googlecode.com/components/driver-component.js:10299)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///var/folders/Ne/NepmGbBUFwK7TxfPj5g0gE+++TI/-Tmp-/tmpSLKAhJ/extensions/fxdriver#googlecode.com/components/driver-component.js:603)
The "play" button is inside an iframe, switch to it before locating the element:
driver.switch_to.frame("app-player")
play = driver.find_element_by_id("play-pause")
play.click()
Controlling through Javascript
Is there a JS SDK for the player? Can I send play/pause commands using Javascript?
The Play button doesn’t have any observable events, nor does it listen for any. Playback can only be triggered by the user clicking the button.
We’re certainly looking at creating a JS SDK; it’s a bigger project and right now we’re focusing on quickly releasing APIs to open up the general Spotify ecosystem (catalogue, playlists, etc).
None of these requests are falling on deaf ears, and we’re working hard to bring out as much functionality as we can.
From Sptotify's Dev Page
Related
This question is very similar to Selenium webdriver can't find elements at chrome://downloads
I'm trying to use Selenium with Python (3) to get at the cancel button on the chrome://downloads page. My use case is that I have an obfuscated link for which a random token is generated every time a user clicks on it. If you don't click on it, you can't start the download (it seems to fire a piece of js that generates the token, but I haven't been successful in digging through the code to figure out how that happens).
For my test to pass, all I need is to verify that:
The download starts (and doesn't give a 404), and
The file it's trying to download is the right size.
The way that I'm trying to accomplish this is by triggering the download by clicking on the button element, then having Selenium open chrome://downloads, cancel the download, and capture the file size of the file that it attempted to download.
In theory this seems like it should work, the stumbling block is trying to access any elements in the #shadow-root tags on the chrome://downloads page. The solution to the other question which I linked above unfortunately no longer works:
driver = webdriver.Chrome("chromedriver.exe")
driver.get("chrome://downloads/")
manager = driver.find_element_by_css_selector('body/deep/downloads-manager')
item = manager.find_element_by_css_selector('body/deep/downloads-item')
shadow = driver.execute_script('return arguments[0].shadowRoot;', item)
link = shadow.find_element_by_css_selector('div#title-area>a')
file_url = link.get_attribute("href")
... as it fails on the item declaration line:
>>> item = manager.find_element_by_css_selector('body/deep/downloads-item')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 430, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 659, in find_element
{"using": by, "value": value})['value']
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/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":"css selector","selector":"body/deep/downloads-item"}
(Session info: chrome=80.0.3987.149)
This is out of my area of expertise, and any help in figuring out how to get at the cancel button would be greatly appreciated.
Please try the below.
driver.execute_script("document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector("cr-button[focus-type='cancel']").click()")
If you need more information how to work on the shadow-root elements, please refer here. And if you want to work on the downloads then you can refer to this and update the js as per your requirement.
Iam tested webscraping the page:http://www.guiadosquadrinhos.com/todas-capas-disponiveis
I need navigate in pagination geting info pages favorite.
I need click link javascript next page:
javascript:__doPostBack('ctl00$MainContent$lstProfileView$dataPagerNumeric2$ctl02$ctl00')
from selenium import webdriver
import time
driver = webdriver.PhantomJS(executable_path='C:\Python27\Tools\phantomjs\phantomjs.exe')
driver.get("http://www.guiadosquadrinhos.com/todas-capas-disponiveis")
#print(driver.find_elements_by_class_name("numero_capinha")[0].text)
#driver.find_elements_by_class_name("next_last")[0].click()
#time.sleep(5)
print(driver.find_elements_by_class_name("numero_capinha")[0].text)
driver.find_elements_by_class_name("next_last")[0].click()
print(driver.find_elements_by_class_name("numero_capinha")[0].text)
My code return:
sobreontem - Independente
Traceback (most recent call last): File "teste_selenium.py", line 10, in
driver.find_elements_by_class_name("next_last")[0].click() File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 74, in click
self._execute(Command.CLICK_ELEMENT) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 453, in _execute
return self._parent.execute(command, params) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py",
line 201, in execute
self.error_handler.check_response(response) File "C:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py",
line 181, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.ElementNotVisibleException: Message:
{"errorMessage":"Element is not currently visible and may not be
manipulated","request":{"headers":{"Accept":"application/json","Accept-Encoding":"identity","Connection":"close","Content-Length":"81","Content-Type":"application/json;charset=UTF-8","Host":"127.0.0.1:63160","User-Agent":"Python-urllib/2.7"},"httpVersion":"1.1","method":"POST","post":"{\"sessionId\":
\"5d3cfdc0-5d3b-11e5-b784-67706273a0bb\", \"id\":
\":wdc:1442494581220\"}","url":"/click","urlParsed":{"anchor":"","query":"","file":"click","directory":"/","path":"/click","relative":"/click","port":"","host":"","password":"","user":"","userInfo":"","authority":"","protocol":"","source":"/click","queryKey":{},"chunks":["click"]},"urlOriginal":"/session/5d3cfdc0-5d3b-11e5-b784-67706273a0bb/element/%3Awdc%3A1442494581220/click"}}
Screenshot: available via screen
Whats is problem ?
Since, you are doing the following -
driver.find_elements_by_class_name("next_last")[0].click()
If you look in the source, there are multiple elements with this class name and the first one is disabled, because that is for the previous button and you are on first page.
I have a form to be completed and a button to be pressed on a page which would take me to another URL(post form completion) and that page has another button upon clicking which the results filled in the form will be downloaded in a well arranged pdf format. I am currently having difficulty in navigating to the second URL upon clicking the first URL's button, that is, when I try to select any second window's component, I get the following error.
Traceback (most recent call last):
self.driver.find_element_by_css_selector('.buttons.button-save').click()
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 376, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 664, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 175, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 166, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"css selector","selector":".buttons.button-save"}
I also tried using explicit wait before I try to click on the second page's button, but it didn't work.
Any help would be appreciated, thanks !
Edit :
When I took a screenshot after clicking the button on the first page and saved the image, I could only see a screenshot of the first URL. Also, I added an explicit wait to locate the presence of an element in the second URL and I got the following traceback
Traceback (most recent call last):
EC.presence_of_element_located((By.ID, "button-save")))
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/support/wait.py", line 75, in until
raise TimeoutException(message, screen, stacktrace)
Relevant Code,
#Clicking the save button
self.driver.find_element_by_css_selector('.submit.submit.submit').click()
#Waiting to click on the button in second URL
element = WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.ID, "button-save")))
#Saving the screenshot to check in which window it is
self.driver.save_screenshot('file2.png')
#Click the button on the second page
self.driver.find_element_by_css_selector('.buttons.button-save').click()
I'm using webdriver to click on a submit button on a form in an iframe. Basically:
self.driver.find_element_by_css_selector("[name~='field_1']").clear()
self.driver.find_element_by_css_selector("[name~='field_1']").send_keys("123")
self.driver.find_element_by_css_selector("[name~='field_1']").send_keys(Keys.RETURN)
self.driver.switch_to_window(self.driver.window_handles[-1])
self.assertEqual(self.driver.current_url, "http://fake_address.com")
I've also tried:
self.driver.find_element_by_css_selector("[name~='field_1']").clear()
self.driver.find_element_by_css_selector("[name~='field_1']").send_keys("123")
self.driver.find_element_by_css_selector("#submit-endslide").click()
self.driver.switch_to_window(self.driver.window_handles[-1])
self.assertEqual(self.driver.current_url, "http://fake_address.com")
and:
self.driver.find_element_by_css_selector("[name~='field_1']").clear()
self.driver.find_element_by_css_selector("[name~='field_1']").send_keys("123")
self.driver.find_element_by_css_selector("#submit-endslide").submit()
self.driver.switch_to_window(self.driver.window_handles[-1])
self.assertEqual(self.driver.current_url, "http://fake_address.com")
I've been getting:
Traceback (most recent call last):
File "test_ytplayer_smoke_form.py", line 198, in testSmallFormSubmission
self.driver.find_element_by_css_selector("[name~='field_1']").send_keys(Keys.RETURN)
File "/home/giant/our_player/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 293, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "/home/giant/our_player/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 370, in _execute
return self._parent.execute(command, params)
File "/home/giant/our_player/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 166, in execute
self.error_handler.check_response(response)
File "/home/giant/our_player/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response
raise exception_class(message, screen, stacktrace)
TimeoutException: Message: u'Selenium took too long to run your command.
Then
WebDriverException: Message: u'Due to a previous error, this job has already finished.
Regardless of which method I choose, I will always get a timeout when clicking the submit button;
(I've already tried increasing the timeout to 300)
Note:When I watch the test run, the submit button is being clicked and a new tab will open, but the test will never pass due to the exceptions.
There seemed to be a browser specific issue Chrome V28 that was causing this issue; I updated to the latest browser version Chrome V35 and this seemed to be resolved
As my comment says, we need more info, but comments are ugly to edit, so I add more info here:
Instead of wait based on time, try WebDriverWait see here for start.
Store element in variable when found, like:
field_1 = self.driver.find_element_by_css_selector("[name~='field_1']")
field_1.clear()
field_1.send_keys("123")
submit = self.driver.find_element_by_css_selector("#submit-endslide")
submit.click()
But try to use WebElementDrive and ExpectedConditions instead of driver methods directly.
I want to handle a web dialog box under selenium web driver (Internet Explorer) . I am using Python
In my application when I click on Icon, a web dialog box opens which contains some text boxes (Webelements) and I need to click on save button after entering some text. The problem is I dont know whether the focus got switched to the web dialog box or not. Here is my code
driver.find_element_by_xpath("//img[contains(#src,'/images/btn_add.gif')]").click()
driver.switch_to_alert()
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
Here is the error I am getting
Traceback (most recent call last):
File "C:\Users\vthaduri\workspace\LDC\test.py", line 14, in <module>
driver.find_element_by_name("report_cutoff_date").sendkeys("10/31/2010")
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 282, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 651, in find_element
{'using': by, 'value': value})['value']
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\webdriver.py", line 153, in execute
self.error_handler.check_response(response)
File "C:\Python27\lib\site-packages\selenium-2.21.2-py2.7.egg\selenium\webdriver\remote\errorhandler.py", line 147, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: u'Unable to find element with name == report_cutoff_date'
For your information the webelement is present with same name and cutoff date.
Can some one help me on this.
Try this:
parent_h = browser.current_window_handle
# click on the link that opens a new window
handles = browser.window_handles # before the pop-up window closes
handles.remove(parent_h)
browser.switch_to_window(handles.pop())
# do stuff in the popup
# popup window closes
browser.switch_to_window(parent_h)
# and you're back
I think the problem is with the following code -
driver.switch_to_alert();
You want to switch to another dialog box which appears when you perform the first click() operation. I think that this box that appears is not an alert.
You might have to switch to the other dialog box by using
driver.getWindowHandles();
driver.switchTo().window(handle);
You can check an example here.