How can I reset selenium mouse position with python? - python-3.x

when I use selenium to click element by position with python
1: I can through find_element_by_xpath("//div[#class='close']").click()
to colse some tips
2. I use this ActionChains to click element by code
action = webdriver.common.action_chains.ActionChains(driver)
action.move_by_offset(x_pos, y_pos))
action.click()
action.perform()
but not clicked, I think mouse position have changed buy using fisrt click, and How can I reset mouse position to (0,0) or have mouse position reset method?

Related

PYQT5 Open menu on cursor position in QTextEditor

I have a QTextEditor which I would like to open a menu for user to choose when he press Ctrl+Space.
I was able to capture the key events and create the menu and trigger it.
My problem is to open the menu on the text cursor position.
How do i get the QPoint of the textcursor and not the mouse position?
QTextEdit.cursorRect returns a rectangle (in viewport coordinates) that includes the cursor.
You're probably imlementing autocompletion/intellisense, it which case I'd recommend using QCompleter instead of QMenu. Take a look at Custom Completer Example.
You can override QTextEdit.keyPressEvent or use eventFilter to capture Ctrl+Space shortcuts and call completer.complete(rect) to show popup.

How to "scroll up" in div using selenium in python?

I am trying to scrape job details in LinkedIn. As per now, I am able to scroll till the end of list.
code:
#scrolling till buttom of page(online job section)
target = driver.find_element_by_xpath('//div[#class="jobs-search-results jobs-search-results--is-two-pane"]')
time.sleep(1)
driver.execute_script('arguments[0].scrollTop = arguments[0].scrollHeight', target)
driver.implicitly_wait(1)
time.sleep(1)
now I want to scroll up to the top of the div.
I want to scroll up in div shown as red rectangle:
Scrolling to top of the page in Python using Selenium
Scroll Element into View with Selenium
There is 2 main way to do that:
Firstly, you can locate the element(at the top of your DOM view) in the DOM and then scrolling up until you find the element
element = driver.find_element_by_xpath("element_xpath")
driver.execute_script("return arguments[0].scrollIntoView(true);", element)
Secondly, you can easily use CTRL+HOME for scrolling to the top of the page with the help of Keys
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.HOME)
But in your situation which dealing with JavaScript DOM, recommended way is the first one.
There are many ways to do this, here is one way:
driver.execute_script("window.scrollTo(0, 0);")
Alternatively you can scroll up with the mouse:
import pyautogui
pyautogui.scroll(10)

PyAutoGui - locate doesn't see the right-click menu

I have the code below that basically identify the small chrome icon in the windows toolbar, right-click on it using pyautogui and then it should locate the "New Window" option. The problem I face is that, even if I take a screenshot after the right-click, the small menu doesn't show up, making it impossible to locate the "New Window" option.
# this part works
chrome_small_icon = r"C:\Users\chrome_small_icon.png"
elem = pyautogui.locateOnScreen(chrome_small_icon)
elem_center = pyautogui.center(elem)
pyautogui.click(elem_center, duration=0.5, button="right")
time.sleep(0.5)
im_after_right_click = pyautogui.screenshot()
# this part finds zero element, reason being, the right-click menu is like a ghost...
chrome_new_window = r"C:\Users\new_window_text.png"
elements = pyautogui.locateAllOnScreen(chrome_new_window)
does anybody have any suggestion about how to locate elements inside the menu that appears when we right click on an element?
Thanks
EDIT
it seems this issue happens only if I right click on the windows toolbar. It does work if I right click on other locations of the screen.
Instead of trying to locate the new window text use the keyboard to select the new window option. I just ran the following code on MacOS and I was successfully able to open a new Chrome window:
import pyautogui
import time
pyautogui.rightClick(pyautogui.center(pyautogui.locateOnScreen('chrome.png')))
#chrome.png is an image of the chrome icon
pyautogui.typewrite('new window')
pyautogui.press('enter')

how can I get the text of a hidden element in watir

The html element is p, its attribute style="display:none;"
when the mouse move on the element,it is visible. How can I get the text of p in watir? Thank you!
Try:
#browser.p(:style, 'display:none;').fire_event 'mouseover
#browser.p(:style, 'display:none;').click #change to visible element you want to click
fire_event 'mouseover' acts as if your mouse is hovering over the element

In LWUIT, a white rectangular background appears when the button is pressed, how to remove it?

Using LWUIT, I have a Form (form1) which contains a Button (see the silver arrow at the bottom of the Form in the left image below). This Button is constructed using an image which has a transparent background to give the effect of a non-rectangular Button. When the Button is pressed it shows another Form (form2).
But unfortunately, when the user presses this Button, a white rectangular background appears for a few milliseconds in the background of the image. see the right image below:
The question is:
How may I prevent this white rectangle from being appeared after pressing the Button ?
You have modified the state of the button when it is selected or unselected. You also have to modify pressed style.
You can do it with button.getPressedStyle()
Try:
button.getPressedStyle().setBorder(null);
button.getPressedStyle().setBgTransparency(0);

Resources