How do I click "Copy" using Selenium WebDriver? - python-3.x

How do I click the "Copy" button from this URL https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php
The tag I need to click is tagged as "Copy"
I have tried multiple "find element by" methods however I keep getting errors such as no such element.
button = driver.find_elements_by_class_name('toolbar-item') #not working
driver.findElementByClassName("a.cc_btn.cc_btn_accept_all") #not working
driver.find_element_by_css_selector("toolbar").click() #not working
driver.findElementByClassName("toolbar-item").click() #not working
In this case how to I select the "Copy" ref and what method is used to click "Copy"?
Further, when the "Copy" button is clicked, how can I paste the contents to a text file.
These are the other errors I get.
Errors:
Traceback (most recent call last):
File "untitled.py", line 27, in <module>
driver.find_element_by_css_selector("toolbar-item").click()
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 598, in find_element_by_css_selector
return self.find_element(by=By.CSS_SELECTOR, value=css_selector)
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 978, in find_element
'value': value})['value']
File "/Library/Python/2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Library/Python/2.7/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":"toolbar-item"}
(Session info: chrome=81.0.4044.138)
Also:
NoSuchElementException: Message: Unable to locate element:

To click on the Copy button induce WebDriverWait() and wait for presence_of_element_located() and following xpath.You need to scroll the element to click.
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
driver = webdriver.Chrome()
driver.get("https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php")
copybtn=WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,"//a[text()='Copy']")))
copybtn.location_once_scrolled_into_view
copybtn.click()
You can use following css selector as well.
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
driver = webdriver.Chrome()
driver.get("https://www.w3resource.com/java-exercises/basic/java-basic-exercise-249.php")
copybtn=WebDriverWait(driver,5).until(EC.presence_of_element_located((By.CSS_SELECTOR,".toolbar-item>a")))
driver.execute_script("arguments[0].scrollIntoView();", copybtn)
copybtn.click()

Related

How to click on a button inside a side bar label with Selenium and Python

I´m trying to click a button inside a side bar label:
<span class="sidebar-label">
Exportar Listagem
</span>
Here is my code:
driver.get("https://ispot2.faturaiqos.pt")
html = driver.page_source
exp = driver.find_element_by_link_text("Exportar Listagem")
exp.click()
Here is the error I get:
Traceback (most recent call last):
File "/home/pi/Desktop/Python Testes/Ispot JP.py", line 25, in <module>
exp = driver.find_element_by_link_text("Exportar Listagem")
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 317, in find_element_by_link_text
return self.find_element(by=By.LINK_TEXT, value=link_text)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 745, in find_element
{'using': by, 'value': value})['value']
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "/usr/local/lib/python3.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Exportar Listagem"}
(Session info: chrome=84.0.4147.141)
(Driver info: chromedriver=84.0.4147.141 (80c974bf7990b9735a8e885046fc5c9b1da4796c-refs/branch-heads/4147#{#1132}),platform=Linux 5.4.79-v7+ armv7l)
To click on the element with text as Exportar Listagem you can use the following Locator Strategies:
Using xpath and contains():
driver.find_element_by_xpath("//span[#class='sidebar-label' and contains(., 'Exportar Listagem')]").click()
Using xpath and normalize-space():
driver.find_element_by_xpath("//span[#class='sidebar-label' and normalize-space()='Exportar Listagem']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using xpath and contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='sidebar-label' and contains(., 'Exportar Listagem')]"))).click()
Using xpath and normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='sidebar-label' and normalize-space()='Exportar Listagem']"))).click()
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

selenium TimeoutException: in automating whatsap

i am automating whatsap by using selenium and python script. I am trying to open chat by using xpath.it works well with recent chats, but when i try to open chat from contacts it raises TimeoutException.
here is my code:
import traceback
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import time
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
chrome_browser = webdriver.Chrome(executable_path = 'F:\driver\chromedriver.exe')
chrome_browser.get('https://web.whatsapp.com/')
time.sleep(15)
#print('scanning is completed')
wait = WebDriverWait(chrome_browser , 30)
user_name = "sakshi clg"
#user = chrome_browser.find_element_by_xpath('//span[#title="{}"]'.format(user_name))
#user.click()
#visibility_of_element_located element_to_be_clickable
#wait.until(EC.element_to_be_clickable(By.XPATH , "//span[#title=User_name]"))
try:
user=wait.until(EC.presence_of_element_located((By.XPATH ,"//span[#title='{}']".format(user_name))))
user.click()
except TimeoutException:
traceback.print_exc()
exception details:
Traceback (most recent call last):
File "C:/Users/DELL/PycharmProjects/whatsap_prj/main.py", line 25, in
user=wait.until(EC.presence_of_element_located((By.XPATH ,"//span[#title='{}']".format(user_name))))
File "C:\Users\DELL\PycharmProjects\whatsap_prj\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
please someone help me.I am new to selenium.

Bluetooth error while making gmail login bot using selenium in python. But even the basic search for "Email" id element is also not found

It does not seem to work and keeps giving me an error regarding Bluetooth drivers
Following is my code:-
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
usernameStr = 'putYourUsernameHere'
passwordStr = 'putYourPasswordHere'
browser = webdriver.Chrome()
browser.get(('https://accounts.google.com/ServiceLogin?'
'service=mail&continue=https://mail.google'
'.com/mail/#identifier'))
# fill in username and hit the next button
username = browser.find_element_by_id('Email')
username.send_keys(usernameStr)
nextButton = browser.find_element_by_id('next')
nextButton.click()
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, "Passwd")))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_id('signIn')
signInButton.click()
This is the error of my code:-
[11092:17164:0603/171812.746:ERROR:device_event_log_impl.cc(208)] [17:18:12.747] Bluetooth: bluetooth_adapter_winrt.cc:723 GetBluetoothAdapterStaticsActivationFactory failed: Class not registered (0x80040154)
Traceback (most recent call last):
File "e:/python.py", line 62, in
username = browser.find_element_by_id('Email')
File "C:\Users\Bhaskar\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 360, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "C:\Users\Bhaskar\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
'value': value})['value']
File "C:\Users\Bhaskar\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\Bhaskar\AppData\Local\Programs\Python\Python37\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":"[id="Email"]"}
(Session info: chrome=83.0.4103.61)
The username box doesn't have the id 'Email', but 'identifierId'. The same goes for the 'next' button.
Try something like this (might work a bit different for you, since I have Google in a different language).
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
usernameStr = 'putYourUsernameHere'
passwordStr = 'putYourPasswordHere'
browser = webdriver.Chrome()
browser.get(('https://accounts.google.com/ServiceLogin?'
'service=mail&continue=https://mail.google'
'.com/mail/#identifier'))
# fill in username and hit the next button
username = browser.find_element_by_id('identifierId')
username.send_keys(usernameStr)
nextButton = browser.find_element_by_xpath('//*[#id="identifierNext"]/span/span')
nextButton.click()
After this, Google blocks my attempts...
ID you are looking for is "identifierId". try with that.

Cannot Check a Check-box with selenium

I tried to crawl a website and need to check some checkboxes.
xpath = driver.find_element_by_xpath
for i in range(20):
time.sleep(0.5)
try:
xpath("//*[#id='pub_check_sort3_0']").click()
# checkbox = xpath("//*[#id='pub_check_sort3_0']")
# driver.execute("arguments[0].click();",checkbox)
break
except NoSuchElementException as e:
print('retry')
time.sleep(1)
else:
raise e
Those are the codes that I tried to click a check-box and still cannot click the check-box. Also, I tried not only xpath value, tried with id and class.
<li class="general">
<span class="fCheck">
<input onclick="checkAction(this)" class="selectPL" type="checkbox" id="pub_check_sort3_0" value="025001">
<label for="pub_check_sort3_0"></label>Academic Journal (1,505)</span></li>
Those are HTML codes for the check-box and following is the xpath
//*[#id="pub_check_sort3_0"]
Traceback (most recent call last):
File "/Users/chanhee.kang/Desktop/DBpia/db_sel.py", line 37, in <module>
xpath("//*[#id='pub_check_sort3_0']").click()
File "/Users/chanhee.kang/anaconda3/lib/python3.7/site- packages/selenium/webdriver/remote/webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "/Users/chanhee.kang/anaconda3/lib/python3.7/site- packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "/Users/chanhee.kang/anaconda3/lib/python3.7/site- packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/chanhee.kang/anaconda3/lib/python3.7/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 <input onclick="checkAction(this)" class="selectPL" type="checkbox" id="pub_check_sort3_0" value="025001"> is not clickable at point (73, 555). Other element would receive the click: <div class="filterGroup eToggleSection">...</div>
(Session info: chrome=74.0.3729.169)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729#{#29}),platform=Mac OS X 10.14.0 x86_64)
Those are the errors that I got.
To click() on the desired element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li.general label[for='pub_check_sort3_0']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='general']//label[#for='pub_check_sort3_0']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Python Selenium error - is not clickable at point (207.1666717529297,25) because another element <div id="overley" class="over_ley"> obscures it

I have below code -
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
Create.click()
Whenever I am running this code in script or in batch I am getting below error -
Traceback (most recent call last):
File "P:\Selenium\helpline1.py", line 15, in <module>
Create.click()
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webelement.py", line 628, in _execute
return self._parent.execute(command, params)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 312, in execute
self.error_handler.check_response(response)
File "P:\Selenium\python-3.5.1\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 237, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a id="anc" class="RedirectTab" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("anc", "", true, "", "", false, true))"> is not clickable at point (207.1666717529297,25) because another element <div id="overley" class="over_ley"> obscures it
Login to the site happens perfectly but after that this error in clicking an element (that is the next line of code).
Also when I am running this code line by line manually in Python IDLE or Python CLI it runs without any error. Issue just in script/batch mode in IDLE or Python CLI
Thanks to suggest.
If overlay displayed while you're trying to perform a click, you might need to wait until overlay is closed:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
...
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
WebDriverWait(driver, 10).until_not(EC.visibility_of_element_located((By.ID, "overley")))
Create.click()
Try this:
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
wait = WebDriverWait(driver, 10)
driver.get("url")
username = driver.find_element_by_id("LoginID")
pwd = driver.find_element_by_id("Password")
username.send_keys('usrnme')
pwd.send_keys('pswd')
Login = driver.find_element_by_id("Button2")
Login.click()
element = wait.until(EC.element_to_be_clickable((By.XPath, "//*[#title='Create Incident']")))
Create = driver.find_element_by_xpath("//*[#title='Create Incident']")
Create.click()
By usin a wait, you will give time to browser to load de button, BEFORE try to performe the click, that will happen when the element is prepared.

Resources