Double clicking in python selenium - python

I am using selenium with python. Im able to get the code below to click where I want but I want it to dbl click. Im not very good with the action chains and I know I need that for dbl click. Can anyone help with what I need to change around?
user = self.find_element_by_id("selUsers")
for option in user.find_elements_by_tag_name("option"):
if option.text == "Admin, Ascender":
option.click()

Action chains is the only best option as far i know
from selenium.webdriver.common.action_chains import ActionChains
driver=self.webdriver
user = self.find_element_by_id("selUsers")
for option in user.find_elements_by_tag_name("option"):
if option.text == "Admin, Ascender":
actionChains = ActionChains(driver)
actionChains.double_click(option).perform()

try this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
usernameStr = 'xxxxxx'
passwordStr = 'xxxxx'
browser = webdriver.Chrome()
browser.get(('https://accounts.google.com/ServiceLogin?'
'service=mail&continue=https://mail.google'
'.com/mail/#identifier'))
username = browser.find_element_by_id('identifierId')
username.send_keys(usernameStr)
nextButton = browser.find_element_by_id('identifierNext')
nextButton.click()
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//* [#id="password"]/div[1]/div/div[1]/input')))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_id('passwordNext')
signInButton.click()
apsButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//[#id="gbwa"]/div/a')))
apsButton.click()
driveButton = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.XPATH,'//*[#id="gb49"]/span[1]')))
driveButton.click()

Related

Selenium not updating current window

Selenium seems to be looking for data in an old page and not the new one.
I'm trying to automate a search where I select from a dropdown menu and fill a box with some value
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
url = 'http://www.op.nysed.gov/opsearches.htm'
value = '60'
license_no = '084157'
driver = webdriver.Chrome()
driver.get(url)
select = Select(driver.find_element(By.XPATH, '//form[#id="licensee-num-form"]/center/select'))
select.select_by_value(value)
fill = driver.find_element(By.XPATH, '//form[#id="licensee-num-form"]/center/input')
fill.send_keys(license_no)
fill.send_keys(Keys.ENTER)
data = driver.find_element(By.XPATH, "//div[#id='content_column']")
However, when I print data.text, it prints data from the first page, not the second one. I tried using driver.refresh() to refresh the page but it did not work.
This happens because you getting the data = driver.find_element(By.XPATH, "//div[#id='content_column']") immediately after clicking the Enter. The page still not refreshed. You should add a short delay there and then wait for element visibility on the refreshed page.
Please try this:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
url = 'http://www.op.nysed.gov/opsearches.htm'
value = '60'
license_no = '084157'
driver = webdriver.Chrome()
wait = WebDriverWait(driver, 20)
driver.get(url)
select = Select(driver.find_element(By.XPATH, '//form[#id="licensee-num-form"]/center/select'))
select.select_by_value(value)
fill = driver.find_element(By.XPATH, '//form[#id="licensee-num-form"]/center/input')
fill.send_keys(license_no)
fill.send_keys(Keys.ENTER)
time.sleep(0.5)
data = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#id='content_column']")))

Selenium element is not clickable even after excplicitly waiting for it to be

I'm trying to open a form, wait for all of it's elements to be loaded and fill one of the form's fields.
Form is loading, and I'm setting a WebdriverWait to explicitly wait for my element to be clickable.
But on elem.click() I get exception
Element...is not clickable at the point
Code sample I'm using:
def fill_element(driver, xpath, keys, secs=2000):
print(f"\nFilling for {xpath}...")
elem = WebDriverWait(driver, secs).until(EC.element_to_be_clickable((By.XPATH, xpath)))
elem.clear()
elem.click()
elem.send_keys("")
elem.send_keys(keys)
If I don't use "click" and just trying to send keys - nothing happens. No exceptions and no form filling.
Could someone please advise how to use Wait properly?
P.S. Window was maximized even before clicking on the form to open, so it shoudln't be the problem.
UPD Full code:
from selenium import webdriver
from selenium.webdriver.common.by import By
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 pathlib import Path
import time
def fill_element(driver, xpath, keys, secs=2000):
print(f"\nFilling for {xpath}...")
element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, xpath)))
driver.find_element_by_xpath(xpath).send_keys("test")
def authorize(driver):
email = "xxx"
pwd = "xxx"
my_acc_button = driver.find_element(By.XPATH, "//li[#class='menuOverride showLoginPopUp menu-account-icon']")
my_acc_button.click()
email_xpath = "// INPUT[# type='email']"
fill_element(driver, email_xpath, email)
pwd_xpath = "// INPUT[# type='password']"
fill_element(driver, pwd_xpath, pwd)
login_button = driver.find_element(By.XPATH, "//div[#class='btn green-color']")
# login_button.click()
driver_path = r'D:\Projects\cheater_buster_parser\utils\chromedriver.exe'
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
start_url = 'https://www.cheaterbuster.net/'
driver.get(start_url)
authorize(driver)
According to new updates, use the following:
from selenium import webdriver
from selenium.webdriver.common.by import By
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 pathlib import Path
import time
def fill_element(driver, xpath, keys, secs=2000):
print(f"\nFilling for {xpath}...")
element = WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, xpath)))
driver.find_element_by_xpath(xpath).send_keys("test")
def authorize(driver):
email = "xxx"
pwd = "xxx"
my_acc_button = driver.find_element(By.XPATH, "//li[#class='menuOverride showLoginPopUp menu-account-icon']")
my_acc_button.click()
email_xpath = "(//INPUT[#type='email'])[2]"
fill_element(driver, email_xpath, email)
pwd_xpath = "(//INPUT[#type='password'])[3]"
fill_element(driver, pwd_xpath, pwd)
login_button = driver.find_element(By.XPATH, "//div[#class='btn green-color']")
# login_button.click()
driver_path = r'D:\Projects\cheater_buster_parser\utils\chromedriver.exe'
driver = webdriver.Chrome(driver_path)
driver.maximize_window()
start_url = 'https://www.cheaterbuster.net/'
driver.get(start_url)
authorize(driver)
Use the foloowing code but make sure if this element is button to click or text field then send keys to it. (According to your info i think its text field
def fill_element(driver, xpath, keys, secs=2000):
print(f"\nFilling for {xpath}...")
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "xpath")))
elem=driver.find_element_by_xpath(xpath)
elem.clear()
elem.click()
elem.send_keys("")

How to scroll in Instagram following window using Selenium and Python?

How would I scroll in the following window on Instagram using Selenium and Python? I've tried everything I've found and none of them work.
Here's what I have to get me to the following window:
from selenium import webdriver
browser = webdriver.Firefox(executable_path = '/usr/local/bin/geckodriver')
browser.implicitly_wait(5)
browser.get('https://www.instagram.com/')
sleep(2)
username_input = browser.find_element_by_css_selector("input[name='username']")
password_input = browser.find_element_by_css_selector("input[name='password']")
username_input.send_keys("Enter Username Here")
password_input.send_keys("Enter Password Here")
login_button = browser.find_element_by_xpath("//button[#type='submit']")
login_button.click()
sleep(5)
browser.find_element_by_xpath('/html/body/div[1]/section/main/div/div/div/section/div/button').click()
sleep(2)
browser.find_element_by_css_selector('button.aOOlW:nth-child(2)').click()
browser.get('https://www.instagram.com/instagram/')
sleep(2)
browser.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
sleep(2)
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i=0
while(i<follower_number):
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])```
have a look at this Question. i tried that and worked.
my code
from selenium.webdriver.common.action_chains import ActionChains
import time
import re
from webdriver_manager.chrome import ChromeDriverManager
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(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.instagram.com/")
time.sleep(5)
my_email=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[1]/div/label/input')
my_email.send_keys("")
my_password=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[2]/div/label/input')
my_password.send_keys("")
time.sleep(5)
login=driver.find_element_by_xpath('//*[#id="loginForm"]/div/div[3]')
login.click()
time.sleep(5)
driver.get("https://www.instagram.com/instagram/following/")
driver.find_element_by_css_selector('li.Y8-fY:nth-child(3) > a:nth-child(1) > div:nth-child(1)').click()
time.sleep(5)
# this is not perfect but it will get your work done
# getting the number of followers
follower_number =int( driver.find_elements_by_xpath('//span [#class="g47SY "]')[2].text)
i = 0
# looping for the exact number of followers ...
while(i<follower_number):
# as the website is dyanamic so updating the follwers list and also the webelement
element = driver.find_elements_by_xpath("//div[#role='dialog']//ul//li")
# executing scroll into view script to view the element and thats gonna load the next element(follower ) ..ultimately your scrolling achived
driver.execute_script("arguments[0].scrollIntoView(true);",element[i])
time.sleep(2)
print(i)
i=i+1

Why does my Selenium URL_to_be statement not work?

Selenium does not seem to register that I manually go to the publish0x.com page.
Does anyone know a solution?
My goal is to manually do the captcha at the login page and afterwards, when I log in and land on the main page I want the script to resume.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import sys
from selenium.webdriver.support.ui import WebDriverWait
def waitForLoad(inputXPath):
Wait = WebDriverWait(driver, 10)
Wait.until(EC.presence_of_element_located((By.XPATH, inputXPath)))
email = '
password = '
options = Options()
options.add_experimental_option("detach", True)
options.add_argument("--window-size=1920,1080")
## options.add_argument("user-data-dir=/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get('https://www.publish0x.com/login')
waitForLoad('//*[#id="email"]')
E_Mail_vak = driver.find_element_by_xpath('//*[#id="email"]')
E_Mail_vak.send_keys(email)
Pass_vak = driver.find_element_by_xpath('//*[#id="password"]')
Pass_vak.send_keys(password)
frame = driver.find_element_by_xpath('//iframe[contains(#src, "recaptcha")]')
driver.switch_to.frame(frame)
Captcha = driver.find_element_by_xpath("//*[#id='recaptcha-anchor']")
Captcha.click()
wait = WebDriverWait(driver, 500)
wait.until(EC.url_to_be("publish0x.com"))
driver.get('https://www.publish0x.com/newposts')
post = driver.find_element_by_css_selector('#main > div.infinite-scroll > div:nth-child(1) > div.content')
title = post.find_element_by_css_selector('h2 > a').text
author = post.find_element_by_css_selector('p.text-secondary > small:nth-child(4) > a').text
title.click()
slider = driver.find_element_by_xpath('//*[#id="tipslider"]')
There are two ways I can think of, one by adding an Input statement like this:
options.add_argument('--disable-gpu')#For properly seeing the outputs
input("Please do the captcha and press any key...)
In this way, the user would complete the data and then press any key for the script to continue.
The other way is by adding a try and except statement.
try:
driver.find_element_by_id("some-element")
except NoSuchElementException:
#do something like
print("Captcha Failed or Incomplete...")
In this, replace the element id "some-element" with any element that is present and only present after the user logs in, for e.g elements like Profile_nav or Settings are only present when someone logs in. So if the element doesn't exist then it would mean that the user didn't complete the captcha.

Milonic Menu: Element is not clickable at point. Other element would receive the click

I am trying to automate the selection of an item in a Milonic menu. I am using code like this:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
driver = webdriver.Firefox()
example_menu = driver.find_element_by_id('m_example')
example_menu.click()
# Wait for the "Example Choice" choice to appear
choice_present = expected_conditions.presence_of_element_located((By.LINK_TEXT, 'Example Choice'))
WebDriverWait(driver, 5).until(choice_present)
# Click on "Example Choice"
example_choice = driver.find_element_by_link_text('Example Choice')
example_choice.click()
However, a WebDriverException is raised with a message like this:
Element is not clickable at point (236, 44). Other element would receive the click
The solution is to use ActionChains to move the mouse over the menu entry but click on the Milonic link:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Firefox()
example_menu = driver.find_element_by_id('m_example')
example_menu.click()
# Wait for the "Example Choice" choice to appear
choice_present = expected_conditions.presence_of_element_located((By.LINK_TEXT, 'Example Choice'))
WebDriverWait(driver, 5).until(choice_present)
# Use an action chain to move to the "Example Choice" but click on the
# Milonic menu link
example_choice = driver.find_element_by_link_text('Example Choice')
mmlink1 = driver.find_element_by_id('mmlink1')
action_chain = ActionChains(driver)
action_chain.move_to_element(example_choice)
action_chain.click(mmlink1)
action_chain.perform()

Categories