How to stop the automatic scroll-down when user scroll up? - python

I want to stop the automatic scroll down when user scroll up by mouse. I have made a function that scroll's down the webpage automatically after every 2 second but I want to stop that automatic scroll when user scroll up by mouse
My code:
from selenium import webdriver
import time
driver=webdriver.Chrome()
driver.get("https://YouTube.com")
driver. maximize_window()
a=input('press y if you want to scroll down')
if a=='y':
while(True) :
driver. execute_script("window.scrollBy(0, 150) ", "")
time.sleep(2)
else:
Print(" Ok")

One of the ways in which you can solve this is using the window.pageYOffset property.
From the Docs
The read-only Window property pageYOffset is an alias for scrollY; as such, it returns the number of pixels the document is currently scrolled along the vertical axis (that is, up or down) with a value of 0.0, indicating that the top edge of the Document is currently aligned with the top edge of the window's content area.
So what you will have to do is each time you scroll in the loop keep a count of the increase in y and for the condition of your loop, you should check that the pageYOffset == YScrolled. If it equals there's no user input, otherwise there is.
There is a good example here

Related

How to get the names of the windows that are open and switch to a target window?

I have been searching everywhere on how to do this specifically but to no avail.
Let's say I want to get a list of all the open windows/apps on my device:
The next thing I want to do is to search for a window with a name "Notepad", or maybe "Facebook", then switch and maximize those window as the main window on the screen.
I've been using pyautogui module for switching tabs by automatically pressing alt + tab keys with the module. However, I believe it'll be prone to mistakes if I have no way of checking which tab/window is currently selected or maximized.
I was thinking of a solution that I can just continuously automate pressing alt + tab until the target window name is in the current active window, but I don't know how to get the name of the current active window as well.
Thank you for the help in advance.
I just found out that pyautogui has a method to get the active window title as well.
import pyautogui
import time
target_window = 'notepad'
count = 0
while True:
window_title = pyautogui.getActiveWindowTitle()
if target_window not in window_title:
count += 1
with pyautogui.hold('alt'):
for _ in range(0, count):
pyautogui.press('tab')
time.sleep(0.25)

Python selenium - drag and drop slide bar without any iframe

I am practising my test automation on seleniumeasy.com
I reached drag and drop slide bar but couldn't figure out how to move the bar value from 10 to 80.
This is the code which I wrote but failed:
drag_ten = driver.find_element_by_xpath("//input[#value=10]")
drop_eighty = driver.find_element_by_xpath("//input[#value=80]")
ActionChains(driver).drag_and_drop(drag_ten, drop_eighty).perform()
Any help will be much appreciated.
One slider is still a single web element. Only the attributes values of that element are changing, when you shift the slider left or right. So in your code, drop_eighty would be undefined at the time of execution.
You could use drag_and_drop_by_offset or click_and_hold and specify the offset to slide the slider
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get('https://www.seleniumeasy.com/test/drag-drop-range-sliders-demo.html')
slider = driver.find_element_by_xpath('//div[#id="slider1"]//input')
ActionChains(driver).drag_and_drop_by_offset(slider, 250, 0).perform()
# Alternative: ActionChains(driver).click_and_hold(slider).move_by_offset(250, 0).release().perform()
However, finding a proper offset value is not that simple. It may also vary based on the window size. There are ways to calculate it, but it would complicate the answer too much.
A simpler option, in that case, would be to use the JavascriptExecutors execute_script method to change the value and then trigger the onchange listener. This option uses more meaningful values but it is specific to your slider implementation and it would need to be adjusted for any other slider implementations.
driver.execute_script('arguments[0].value = 80;', slider)
driver.execute_script('arguments[0].onchange();', slider)

Selenium Python Chrome: Last element in drop down list is not visible in Headless mode

I want to use Selenium to click an element in a menu drop down list. Unfortunately the element I want to click is all the way at the bottom of the list. My Selenium Python Chrome code runs perfectly in foreground mode, but when I run it in background / headless mode I get a timeout, or not clickable error (see notes below). Here is the code, using the actual public url, so you can test it out if you want:
# define chrome options
options = webdriver.ChromeOptions()
# make sure browser is maximized so all elements are visible
options.add_argument('--start-maximized')
# define driver
driver = webdriver.Chrome(chrome_options=options)
# define function used to get button
def getbutton(driver, xpath, waittime=10):
return WebDriverWait(driver, waittime).until(EC.element_to_be_clickable((By.XPATH, xpath)))
# navigate to url
driver.get('http://oasis.caiso.com/mrioasis/logon.do')
# another two different ways to make sure browser window is maximized
# I'm going through this trouble just in case selenium needs the window
# maximized to see the element
driver.fullscreen_window()
driver.maximize_window()
# choose dropdown menu
button1 = getbutton(driver, "//span[contains(#id, 'LBL_')][text()='ATLAS REFERENCE']", waittime=120)
hover = ActionChains(driver).move_to_element(button1)
hover.perform()
time.sleep(5)
# see about navigating to the second to last button in the drop down
# menu list - not what I want, but a test
testing = True
if testing:
# move to second to last button - interestingly this works in foregraound AND background / headless modes
button1a = getbutton(driver, "//span[contains(#id, 'LBL_')][text()='Intertie Scheduling Limit and Tie Mapping']")
hover = ActionChains(driver).move_to_element(button1a)
# THIS WORKS in foreground AND backgroubd modes
hover.perform()
time.sleep(10)
# Now try and click the button I really want - the LAST button in the drop down menu list
button2 = getbutton(driver, "//span[contains(#id, 'LBL_')][text()='Master Control Area Generating Capability List']", waittime=120)
button2.click()
button2 = getbutton fails with the following error message:
selenium.common.exceptions.TimeoutException
Interestingly, if I turn testing to False, I get the following error message:
selenium.common.exceptions.WebDriverException: Message: unknown error:
Element <span ...></table>">Master Control Area Generating Capability List</span>
is not clickable at point (128, 599). Other element would receive the click
It seems like selenium is unable to see the element in background / headless mode, even though neither of these exceptions occur when running in foreground mode.
I'm running Python in Windows Server 2012 R2 for what it's worth.
Thanks for any help!
I've changed a few things in your code and I no longer get the errors. I'm not sure if it's loading the correct webpage though since it's headless and everything seems to be dynamically loaded.
# another two different ways to make sure browser window is maximized
# I'm going through this trouble just in case selenium needs the window
# maximized to see the element
#driver.fullscreen_window()
#driver.maximize_window()
# choose dropdown menu
button1 = driver.find_element_by_xpath("//span[contains(#id, 'LBL_')][text()='ATLAS REFERENCE']")
hover = ActionChains(driver)
hover.move_to_element(button1)
# see about navigating to the second to last button in the drop down
# menu list - not what I want, but a test
testing = True
if testing:
# move to second to last button - interestingly this works in foregraound AND background / headless modes
button1a = driver.find_element_by_xpath("//span[contains(#id, 'LBL_')][text()='Intertie Scheduling Limit and Tie Mapping']")
hover.move_to_element(button1a)
# THIS WORKS in foreground AND backgroubd modes
# Now try and click the button I really want - the LAST button in the drop down menu list
button2 = driver.find_element_by_xpath("//span[contains(#id, 'LBL_')][text()='Master Control Area Generating Capability List']")
hover.click(button2)
hover.perform()
I'm selecting the elements with .find_element_by_xpath() and I'm queuing up all the ActionChains to execute them all at the end with hover.perform().
After further analysis, I'm unable to locate the "masterControlAreaGenCapGrid_GRID_LABEL" element with driver.find_element_by_id("masterControlAreaGenCapGrid_GRID_LABEL").text
It works when the browser isn't headless though...
So I finally figured out what the problem is: Running Windows task scheduler with option 'run whether user is logged on or not' only opens a small browser (1024x768) that CANNOT be resized, even with all the great suggestions being offered here. So I simply cannot see the element I want to click.
See the same issue resolved here: screen resolution in mode "Run whether user is logged on or not", in windows task scheduler
So the less than ideal workaround is to only run when user is logged on.
Thanks for all your help!

PsychoPy Recording Multiple Mouse Clicks

I'm creating an experiment using the PsychoPy coder and am not sure how to implement multiple mouse clicks. In the experiment, there are multiple targets and distractors and during a response period, individuals are to select the targets. However, I currently have it where if you click on one of the targets, you get a correct message, otherwise you get an incorrect message. I also have a function that will wait for a mouse click to find the reaction time and will give the response after the mouse click. How do I add multiple mouse clicks for multiple targets?
def waitForMouse(mouse):
mouse.clickReset()
buttons = mouse.getPressed()
while buttons[0] == False: #wait for mouse click
buttons, times = mouse.getPressed(getTime=True) #get reaction time when mouse clicked
return times[0]
if clock.getTime() >= stimDuration: #start of response period
ResponsePrompt.draw() #indicate to participant to select targets
win.flip()
rt = waitForMouse(myMouse)
if myMouse.isPressedIn(target[0]) or myMouse.isPressedIn(target[1]):
CorrectResp.draw() #selected one of the correct targets
win.flip()
core.wait(2) #allow them to read it
else:
IncorrectResp.draw() #answered incorrectly
win.flip()
core.wait(2)
Presumably you have some way of knowing that the participant is "done." You could just do a while loop until exit condition was met. You would just cycle through your mouse function collecting data and appending it to some list that you will later save until the exit condition is met. The basic pseudo-code approach would be something like:
while 1:
catchData = getMouseClickStuff()
<append catchData to whatever list it should be stored to>
if <endConditionMet>:
break

how to scroll a page inside a qwebview?

I guess this should be a simple task: change a QWebView's content (it always contains several pages of stuff) then scroll the page back in the previous position:
y = self.webView.page().mainFrame().scrollPosition().y()
self.webView.setHtml(looong_html_text)
if y != 0:
self.webView.scroll(0, y)
self.webView.page().mainFrame().scroll(0, y)
self.webView.page().mainFrame().setScrollPosition(QPoint(0, y))
print(self.webView.page().mainFrame().scrollPosition().y())
But the 3 commands inside the if are completely useless: the page scrolls back to top.
What's wrong?
This is a bit of a guess:
setScrollPosition will not let you scroll beyond the end of the page.
Since you are scrolling before the page is displayed, the page's effective height is 0 at the time. You could verify this by checking self.webView.page().mainFrame().contentSize()
Maybe you should do the scrolling when contentSizeChanged is triggered.
use this code
ui->webView->page()->mainFrame()->scroll(ui->webView->page()->mainFrame()->contentsSize().width(), ui->webView->page()->mainFrame()->contentsSize().height());

Categories

Resources