Weibo login in selenium in python? - python-3.x

I'm doing weibo login in selenium, but I can't handle window popup.
This is my code. What is problem?
from selenium import webdriver
username = 'your id'
password = 'your password'
driver = webdriver.Firefox()
driver.get("http://overseas.weibo.com/")
driver.implicitly_wait(10)
handles = driver.window_handles
driver.find_elements_by_link_text('登入微博')[0].click()
driver.implicitly_wait(10)
driver.switch_to_alert()
driver.find_element_by_name('memberid').send_keys(username)
driver.find_element_by_name('passwd').send_keys(password)
driver.find_elements_by_link_text('登入')[0].click()
Traceback (most recent call last):
File "D:/python34/weibo_login.py", line 35, in
driver.find_element_by_name('memberid').send_keys(username)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 362, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 744, in find_element
{'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: {"method":"name","selector":"memberid"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///C:/Users/hena/AppData/Local/Temp/tmpwk788t0k/extensions/fxdriver#googlecode.com/components/driver-component.js:10770)
at fxdriver.Timer.prototype.setTimeout/<.notify (file:///C:/Users/hena/AppData/Local/Temp/tmpwk788t0k/extensions/fxdriver#googlecode.com/components/driver-component.js:625)

Actually opened login form is inside an iframe. It's not an alert. You need to switch this particular iframe first before find element and sendKeys as below :-
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
username = 'your id'
password = 'your password'
driver = webdriver.Firefox()
driver.get("http://overseas.weibo.com/")
wait = WebDriverWait(browser, 10)
link = wait.until(EC.visibility_of_element_located((By.LINK_TEXT, "登入微博")))
link.click()
frame = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "cboxIframe")))
driver.switch_to_frame(frame)
user = wait.until(EC.visibility_of_element_located((By.ID, "memberid")))
user.send_keys(username)
passwd = wait.until(EC.visibility_of_element_located((By.ID, "passwd")))
passwd.send_keys(password)
button = wait.until(EC.visibility_of_element_located((By.ID, "login")))
button.click()
Hope it helps...:)

Related

Invisible intercepting element

I am trying to browse the chefsteps account (this one worked) and click the chefsteps account in my instagram account (the clicking part does not work). But I got "Element Click Intercepted Exception". There is no visible dialog box, but the 'chefsteps' button (element click) is intercepted. What should I do to fix this?
Traceback (most recent call last):
File "C:\Users\DELL\PycharmProjects\Day52_instagram_followers_bot\main.py", line 65, in <module>
bot.find_followers()
File "C:\Users\DELL\PycharmProjects\Day52_instagram_followers_bot\main.py", line 38, in find_followers
chefsteps.click()
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\DELL\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <div class="_3SOD">...</div> is not clickable at point (207, 135). Other element would receive the click: <div class="jLwSh" role="dialog"></div>
(Session info: chrome=94.0.4606.81)
import selenium.common.exceptions
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
from time import sleep
CHROME_DRIVER_PATH ="C:\Development\chromedriver_win32\chromedriver.exe"
SIMILAR_ACCOUNT = "chefsteps"
INSTAGRAM_EMAIL = os.environ['YOUR_INSTAGRAM_EMAIL']
INSTAGRAM_PASSWORD = os.environ['YOUR_INSTAGRAM_PASSWORD']
class InstaFollower:
def __init__(self):
self.driver = webdriver.Chrome(executable_path=CHROME_DRIVER_PATH)
def login(self):
self.driver.get('https://www.instagram.com/')
sleep(3)
email = self.driver.find_element_by_name('username')
email.send_keys(INSTAGRAM_EMAIL)
password = self.driver.find_element_by_name('password')
password.send_keys(INSTAGRAM_PASSWORD)
password.send_keys(Keys.ENTER)
pass
def find_followers(self):
sleep(3)
notif = self.driver.find_element_by_xpath('/html/body/div[5]/div/div/div/div[3]/button[2]')
notif.click()
browser = self.driver.find_element_by_class_name("x3qfX")
browser.send_keys(SIMILAR_ACCOUNT)
sleep(7)
try:
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
chefsteps.click()
except selenium.common.exceptions.ElementClickInterceptedException:
chefsteps = self.driver.find_element_by_css_selector('._4EzTm div')
chefsteps.click()
def follow(self):
sleep(7)
followers_button = self.driver.find_element_by_class_name('-nal3')
followers_button.click()
sleep(5)
number_of_followers = int(followers_button.get_attribute("title").replace(",", ""))
print(number_of_followers)
n = 1
while n < number_of_followers:
try:
follow_buttons = self.driver.find_elements_by_class_name("y3zKF")
for i in follow_buttons:
sleep(1)
i.click()
except selenium.common.exceptions.ElementClickInterceptedException:
cancel = self.driver.find_element_by_class_name("HoLwm")
cancel.click()
continue
bot = InstaFollower()
bot.login()
bot.find_followers()
bot.follow()
Try like below:
1: Not sure where the button appears, If the Element appears after Scrolling:
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
self.driver.execute_script("arguments[0].scrollIntoView(true);",chefsteps)
chefsteps.click()
2: Can use ActionsChains:
from selenium.webdriver import ActionChains
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
actions = ActionChains(self.driver)
actions.move_to_element(chefsteps).click().perform()
3: Use Javascript:
chefsteps = self.driver.find_element_by_class_name('uL8Hv')
self.driver.execute_script("arguments[0].click();",chefsteps)
If none of the above methods work, check if the Element is in an Iframe or shadow-root. And also check for the locator you are using, It should be unique in the DOM that is 1/1.

Error while using Drop-down - Python Selenium

Hi All
Im trying to select a value from drop-down using class_name/xpath
but it's not working. Tried using id but found that the id keeps
changing. Need assistance on this.
This is the dropdown where im trying to select short description
Below is my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import Select
import time
username = '*****'
password = '*****'
url = 'https://*****.service-now.com/'
driver = webdriver.Chrome("C:\WebDrivers\chromedriver.exe")
driver.get(url)
driver.switch_to.frame('gsft_main')
driver.maximize_window()
driver.find_element_by_id('user_name').send_keys(username)
driver.find_element_by_id('user_password').send_keys(password)
driver.find_element_by_id('sysverb_login').click()
wait = WebDriverWait(driver, 10)
incident = wait.until(EC.visibility_of_element_located((By.XPATH, '//span[contains(text(),
"Incident")]')))
incident.click()
open = driver.find_element_by_link_text('Open - Unassigned')
open.click()
time.sleep(25)
element = driver.find_element_by_class_name('form-control default-focus-outline')
dropdown.Select(element)
dropdown.select_by_value('short_description')
Below is the element that I copied and got.
Copy Element:
<select id="d5f43fd407945410af12f2ae7c1ed05c_select" class="form-control default-focus-outline" aria-
expanded="false"><option value="zztextsearchyy" selected="SELECTED" role="option">for text</option>
<option value="number" role="option">Number</option><option value="opened_at"
role="option">Opened</option><option value="short_description" role="option">Short
description</option><option value="caller_id" role="option">Caller</option><option value="priority"
role="option">Priority</option><option value="state" role="option">State</option><option
value="category" role="option">Category</option><option value="assignment_group"
role="option">Assignment group</option><option value="assigned_to" role="option">Assigned to</option>
<option value="sys_updated_on" role="option">Updated</option><option value="sys_updated_by"
role="option">Updated by</option></select>
By xpath:
//*[#id="d5f43fd407945410af12f2ae7c1ed05c_select"]
Gives me the below error:
DevTools listening on ws://127.0.0.1:49915/devtools/browser/30b98d3d-0779-4d85-86bc-9af3a24726a2
[7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)] XXX Init()
Traceback (most recent call last):
File "Test.py", line 39, in <module>
element = driver.find_element_by_class_name('form-control default-focus-outline')
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 564, in
find_element_by_class_name
return self.find_element(by=By.CLASS_NAME, value=name)
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in
find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Python38\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python38\Lib\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":".form-control default-focus-outline"}
(Session info: chrome=81.0.4044.113)
The above message has two different errors:
1) [7012:3772:0417/104241.308:ERROR:browser_switcher_service.cc(238)]
XXX Init()
2) Unable to locate
element: {"method":"css selector","selector":".form-control default-focus-outline"}
Need help on how do I resolve these issues.
Try with WebDriverWait to resolve your issue. If you are still facing issue then check if your element is present within iframe
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.XPATH, "//select[#id='form-control default-focus-outline']/option[text()='Short description']")))
Note : please add below imports to your solution
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
Working solution :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
chrome_options = webdriver.ChromeOptions()
options = Options()
chrome_options.add_argument("--disable-notifications")
driver = webdriver.Chrome(executable_path="C:\New folder\chromedriver.exe",chrome_options=chrome_options)
username = '********'
password = '********'
url = 'https://*******.service-now.com/'
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 20)
iframe = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, 'gsft_main')))
driver.switch_to.frame(iframe)
wait.until(EC.presence_of_element_located((By.ID, "user_name"))).send_keys(username)
wait.until(EC.presence_of_element_located((By.ID, "user_password"))).send_keys(password)
wait.until(EC.presence_of_element_located((By.ID, "sysverb_login"))).click()
driver.switch_to.default_content()
wait.until(EC.element_to_be_clickable((By.XPATH, "//input[#id='filter']"))).send_keys("Incident")
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[#class='sn-widget-list-title ng-binding'][contains(text(),'Open - Unassigned')]"))).click()
iframe = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.ID, 'gsft_main')))
driver.switch_to.frame(iframe)
print wait.until(EC.element_to_be_clickable((By.XPATH, "//h2[contains(#class,'navbar-title list_title')]"))).text
element=wait.until(EC.element_to_be_clickable((By.XPATH, "//span[#class='input-group-addon input-group-select']//select")))
element=wait.until(EC.presence_of_element_located((By.XPATH, "//span[#class='input-group-addon input-group-select']//select")))
all_options = element.find_elements_by_tag_name("option")
for option in all_options:
print("Value is: %s" % option.get_attribute("value"))
option.click()
select = Select(element)
select.select_by_visible_text("Short description")

Python selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

within the website https://isapps.acxiom.com/optout/optout.aspx#section8 I want to access the field “Who is opting out?”. Using the logic of the post Python Selenium webdriver: element not interactable: Element is not currently visible and may not be manipulated I tried following code
Version 1:
ele2 = driver.find_element_by_xpath("//select[#id='Identity']/option[#value='Myself']")
driver.execute_script("arguments[0].click()",ele2)
Version 2:
driver.find_element_by_xpath("//select[#id='Identity']/option[#value='Myself']").click()
The error I get is:
Traceback (most recent call last):
File "website-functions/acxiom.py", line 51, in <module>
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
File "website-functions/acxiom.py", line 30, in acxiom_DD_formfill
driver.find_element_by_xpath("//select[#id='Identity']/option[#value='Myself']").click()
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.6/dist-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":"xpath","selector":"//select[#id='Identity']/option[#value='Myself']"}
(Session info: headless chrome=80.0.3987.87)
This does not make sense to me since the id is indeed “Identity” (check at https://isapps.acxiom.com/optout/optout.aspx#section8).
Here is the full code I used:
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.chrome.options import Options
import os
import time
def acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email):
chrome_options = Options()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
chrome_options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(options=chrome_options)
driver.set_page_load_timeout(10)
driver.set_window_size(1124, 850) # set browser size.
# link to data delete form
print("opening data delete form")
driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
#Select opt out segment: Following option values: "Mail", "Telemarketing", "Email"
ele = driver.find_element_by_xpath("//select[#id='OptOutChoices2']/option[#value='Mail']")
driver.execute_script("arguments[0].click()",ele)
print("dropdown selected")
#Select identity: Following option values: "Myself", "Legal guardian", "Deceased person"
#ele2 = driver.find_element_by_xpath("//select[#id='Identity']/option[#value='Myself']")
#driver.execute_script("arguments[0].click()",ele2)
"""Version 2"""
#driver.find_element_by_xpath("//select[#id='Identity']/option[#value='Myself']").click()
dropdown_optoutchoice=driver.find_element_by_id("'Identity'").location_once_scrolled_into_view
dropdown_optoutchoice.select_by_value('Myself')
# KEEP THIS DISABLED BC IT ACTUALLY SUBMITS
# driver.find_element_by_id("SubmitButton2").send_keys(Keys.ENTER)
print("executed")
time.sleep(4)
driver.quit()
return None
title="Mr"
middlename=""
firstname = "Joe"
lastname = "Musterman"
suffix=""
email = "joe#musterman.com"
acxiom_DD_formfill(title, firstname, middlename, lastname, suffix, email)
Thank you for your help!
Please refer below solution to select value from dropdown box. You can pass your option value and select it using drop down.
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.Chrome("C:\New folder\chromedriver.exe")
driver.get("https://isapps.acxiom.com/optout/optout.aspx#section8")
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//select[#id='Identity']/option[contains(text(),'Who is opting out?')]"))).click();
Here is the correct xpath.
//select[#name='Identity']/option[#value = 'Submitter']
Screenshot:
value attribute value is Submitter not Myself, which is text of the option node. That's why you are getting the error.
ele = driver.find_element_by_xpath("//select[#name='Identity']/option[#value = 'Submitter']")
driver.execute_script("arguments[0].click()",ele) # using js click so that item will be selected though it's not visible.

Selenium - Element not found - iframe issue

Running a selenium script to do some automated testing on servicenow
Getting an element not found error when trying to populate a field on the webpage. The login page has an iframe. But after login, the next page I dont think has an iframe. Also tried driver.switch_to.default_content() but this didnt seem to help.
I know the element is there and has that ID because ive looked at the html. Also tried populating a couple of other fields but had the same issue.
Any suggestions? Thanks.
Originally the url it tries to go to is - https://dev85222.service-now.com/incident.do, but before that the browser goes to the login page which is
https://dev85222.service-now.com/navpage.do, then after logging in, you get directed to incident.do. Once the script gets the the second url it produces the error -Element not found
I think it might be to do with switching iframes
The code -
from selenium import webdriver
import time
import unittest
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import date
from selenium.webdriver.common.action_chains import ActionChains
class IncidentCreate(unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
def test_selenium(self):
#
identifier = "AUTOMATED TESTING"
module = "INCIDENT"
action = "CREATE"
job_name = ""
today = str(date.today())
driver = self.driver
base_url = "https://dev85999882.service-now.com/incident.do"
driver.get(base_url)
driver.implicitly_wait(5)
driver.switch_to.frame("gsft_main")
username = driver.find_element_by_id("user_name")
username.send_keys("username")
password = driver.find_element_by_id("user_password")
password.send_keys("password")
password.send_keys(Keys.RETURN)
identifier_inc = ("AUTOMATED TESTING - INCIDENT - %s" %today)
driver.switch_to.default_content()
time.sleep (10)
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "incident.category"))
)
except:
print("Element not found")
The error - Element not found
E
======================================================================
ERROR: test_selenium (main.IncidentCreate)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:/Users/user/Documents/Automation/FFOX_CLOUD_INC_CREATEv1.py", line 66, in test_selenium
category = driver.find_element_by_id("incident.category")
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 359, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 966, in find_element
'value': value})['value']
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 320, in execute
self.error_handler.check_response(response)
File "C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: [id="incident.category"]
----------------------------------------------------------------------
Ran 1 test in 41.024s
switched to default content, right after clicking enter to login from the first page. This resolved the issue - driver.switch_to.default_content()

How to login to gmail using chrome in windows using python

i am using python script for login to gmail in chrome, but after clicking next the code returning error, can anyone please help me what's wrong, i am new to python. below is the code which i used.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.select import Select
import threading
import os,time,csv,datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
a = webdriver.Chrome()
a.get("https://accounts.google.com/")
user = a.find_element_by_id("Email")
user.send_keys('username#gmail.com')
login = a.find_element_by_id("next")
login.click()
pwd = WebDriverWait(a, 10).until(EC.presence_of_element_located((By.ID, "Passwd")))
pwd.send_keys('p######')
login = a.find_element_by_id("signIn")
login.click()
clk = WebDriverWait(a, 10).until(EC.presence_of_element_located((By.LINK_TEXT, 'myaccount')))
clk.click()
logout = WebDriverWait(a, 10).until(EC.presence_of_element_located((By.ID, "gb_71")))
logout.click()
Below is the error:
Traceback (most recent call last):
File "C:\Users\mapraveenkumar\Documents\Python\gmail.py", line 20, in
clk = WebDriverWait(a, 10).until(EC.presence_of_element_located((By.LINK_TEXT, 'myaccount')))
File "C:\Users\mapraveenkumar\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
That's most likely because after clicking on the "Next" button, the page takes some time to load and present the Password text field. Try using a wait statement to make sure your script waits until the element can be located.
Example:
...
login = a.find_element_by_id("next")
login.click()
pwd = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "Passwd"))
pwd.send_keys('password')
...

Resources