How to mouse over with Python Selenium 4.3.0? - python-3.x

ENV
Python 3.10
Selenium 4
Hi,
I use :
def findAllByXPath(p_driver, xpath, time=5):
try:
#return WebDriverWait(p_driver, time).until(EC.presence_of_all_elements_located((By.XPATH, (xpath))))
return WebDriverWait(p_driver, time).until(EC.presence_of_all_elements_located((By.XPATH, xpath)))
except Exception as ex:
print(f"ERROR findAllByXPath : {ex}")
posts = findAllByXPath(p_driver,"//article//a",2)
for index in range(0,len(posts)):
p_driver.execute_script("arguments[0].scrollIntoView()", posts[index])
time.sleep(random.uniform(0.5, 0.8))
action.move_to_element(posts[index]).perform()
To make a mouse over and try to get elements.
I am trying to get the number of comments on an Instagram post from a page profile.
If you pass manually the mouse over a post, the number of comments or likes is displayed.
But when I try to do it in python, it doesn't find the element.
I tried the XPath from console
$x(//article//li//span[string-length(text())>0]")
It gives results when I freeze the browser with F8.
action.move_to_element(post).perform() doesn't work? I suspect this version 4.3.0 removed many functions like ActionChains from the Selenium version 3, didn't it?
How can I extract this element?
Or Am I doing something wrong?

ActionChains works nicely in Selenium v4!
I don't have an Instagram account so I cannot try there but you can see by running this code that move_to_element() works and how it works.
The About button (top left corner of stackoverflow homepage) get highlighted.
Remember to don't move your mouse while running of course!
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
actions = ActionChains(driver)
driver.get("https://stackoverflow.com/")
about_button = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH, "//a[#href='https://stackoverflow.co/']")))
actions.move_to_element(about_button)
actions.perform()
EDIT: I am using Selenium 4.4.3 and Python 3.10 but it should work with Selenium 4.3.0 as well.

Related

Python Selenium Locate Element within P tags by XPATH

On the following website (https://play2048.co/) is a game called 2048. After I use Selenium to play it eventually gets to a point where is says Game Over!
When I inspect the element (see picture) I see the word Game Over! in p tags. I wish to be able to capture this text and store it in a Python variable as below:
TextV = wait.until(EC.visibility_of_element_located((By.XPATH, '/html/body/div[2]/div[3]/div[1]/p'))).text
I got the XPath by simply right clicking the text in the p tags (inspect element) and selected copy full XPath but it seems Selenium cannot find this text element when I run the whole code.
Thannks
Personally I use CSS_Selector, because it tends to be more correct, and it's more likely to be successful.
So i would recommend something along the lines:
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
# Your Code Here
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR,'the css selector'))).text

How to zoom in a webpage using Python Selenium in FireFox

I am working on Ubuntu and writing a web automation script which can open a page and zoom in the page. I am using python selenium. Below is the code:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("https://www.wikipedia.org/")
driver.maximize_window()
time.sleep(2)
This opens the browser and opens the wikipedia page. Now I have to do the zoom in functionality. For this I tried below line of code:
driver.execute_script("document.body.style.zoom='150%'")
It didn't work. So I tried below line of code:
driver.execute_script('document.body.style.MozTransform = "scale(1.50)";')
driver.execute_script('document.body.style.MozTransformOrigin = "0 0";')
It kind of worked but the webpage height and width were disturbed and it looks like below:
It should look like below:
Can anyone please suggest some good working solution to zoom in a webpage using python selenium in FireFox.
I didn't find any working solution so what I did is, I used pyautogui to simulate the keys.
pyautogui.keyDown('ctrl')
pyautogui.press('+')
pyautogui.keyUp('ctrl')
This made the webpage zoom to 110%. You can run it again to make it to 120% and so on.
Thanks

Perfom a Keys.ARROW_DOWN in a context menu using selenium

I'm trying to perform a Keys.ARROW_DOWN in selenium but it doesn't want to work, the code open the context menu, but the key arrow_down don't work, example of what I'm doing:
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('http://www.google.com.br')
time.sleep(1)
actions = ActionChains(driver)
actions.context_click().send_keys(Keys.ARROW_DOWN).perform()
chromedriver version 83
Someone can give me a light please of what I'm doing wrong?
Thanks for the help!
You can achieve with the help of pyautogui as shown in the below code
import time
import pyautogui
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.keys import Keys
options = webdriver.ChromeOptions()
options.add_argument( "user-data-dir=C:\\Users\\Sangeeta-Laptop\\AppData\\Local\\Google\\Chrome\\User Data\\Guest Profile");
cdriver = "C:\\Users\\Sangeeta-Laptop\\Downloads\\chromedriver_win32 (4)\\chromedriver"
driver = webdriver.Chrome(executable_path=cdriver, chrome_options=options)
driver.get('http://www.google.com.br')
time.sleep(1)
actions = ActionChains(driver)
actions.context_click().perform()
time.sleep(1)
pyautogui.press("down");
But as 0buz said in the comment, there are multiple ways to achieve your requirement. So please tell us what are you trying to achieve in detail and maybe we all will help you to resolve your issue :)
Keys.ARROW_DOWN within Context Menu
Context Menu initiated through context_click() is generally invoked on a WebElement e.g. a link.
Invoking context_click() on a element opens a browser native context menu which is a browser native operation and can't be managed by Selenium by design.
Conclusion
Using Selenium you won't be able to interact with browser native context menu items using send_keys(Keys.ARROW_DOWN), send_keys(Keys.DOWN), etc.
Reference
You can find a relevant discussion in:
Could not do Arrow down using sendKeys(Keys.ARROW_DOWN) in chrome
When you perform keys.ARROW_DOWN you should enter values into field then you can perform action.

Retrieving elements with custom HTML attributes

I have the following website: https://www.kvk.nl/handelsregister/publicaties/, where I would like to retrieve the login link with Selenium, Scrapy and Python. So for the relevant function, I have the following code:
def start_requests(self):
self.driver = webdriver.Chrome(executable_path=os.path.join(os.getcwd(), "Drivers", "chromedriver.exe"))
self.driver.get(self.initial_url)
test = access_page_wait.until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'a[data-ui-test-class="linkCard_toegangscode"]')))
if test.is_displayed():
print("+1")
else:
print("-1")
However, this does not seem to work, since it just waits 15 seconds and then it stops. It will never reach +1 or -1.
Now my question is, how can we point selenium to the correct element. It also does not seem to work using XPATH find_elements_by_xpath("//a[#data-ui-test-class='linkCard_toegangscode']").
Should I use another selection approach and if yes, which one?
Because there is Frame which stopping you to access the element.Switch_To iframe and then access the element.
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
import os
driver = webdriver.Chrome(executable_path=os.path.join(os.getcwd(), "Drivers", "chromedriver.exe"))
driver.get("https://www.kvk.nl/handelsregister/publicaties/")
driver.switch_to.frame(0)
test=WebDriverWait(driver,10).until(expected_conditions.visibility_of_element_located((By.CSS_SELECTOR, 'a[data-ui-test-class="linkCard_toegangscode"]')))
if test.is_displayed():
print("+1")
else:
print("-1")
Try the above code.It should print what you are looking after.

Selenium WebDriverWait returns an error on Python while web scraping

I am creating a script for Facebook scraping.
I am using Selenium 2.53.6, Gecko driver 0.11.1 with Firefox 50 under Ubuntu 14.04.5 LTS. I have also tried it under Windows 10 using Gecko as well as Chromedriver too, with the same results (that I will describe below) :(
The following code fragment I use that I copied from the original documentation in section Explicitly Waits is:
import datetime, time, sys, argparse
from time import strftime
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
...
...
def main(usrEmail, pwd, numberOfScrolls, secondsWait, searchItem, outFilename):
# Open Firefox Browser
binary = FirefoxBinary('/usr/bin/firefox')
browser = webdriver.Firefox(firefox_binary=binary)
#put browser in specific position
browser.set_window_position(400, 0)
# goto facebook
browser.get("http://www.facebook.com")
#waiting( 5 )
try:
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p")))
finally:
logging("logging in...")
The problem is that I get this error:
File "fb.py", line 86, in main
element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p")))
NameError: name 'By' is not defined
As a workaround I am just using delays in a form time.sleep( delay ) and the program works pretty nice, but it would be better if I had the WebDriverWait feature.
Do I miss something obvious or it is a kind of selenium bug or a web browser feature?
Any help / clarification will be greatly appreciated!
Thanks for your time!!
Could be your missing the definition of By:
from selenium.webdriver.common.by import By

Resources