I need to click on two buttons located in the top command bar in a SharePoint list to complete a CSV download.
I have attempted using the 2 locators below and a explicit wait below with no success.
driver.find_element_by_xpath('//*[#id="appRoot"]/div[1]/div[3]/div/div[2]/div[2]/div[2]/div[1]/div/div/div/div/div/div/div[1]/div[4]/button/span').click()
driver.find_element_by_xpath('//*[#id="id__850-menu"]/div/ul/li[2]/button/div/span').click()
excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
Export button html:
<span class="ms-Button-flexContainer flexContainer-124" data-automationid="splitbuttonprimary"><i data-icon-name="svg/excel_16x1_5.svg" aria-hidden="true" class="ms-Button-icon icon-127"><img alt="" src="https://spoprod-a.akamaihd.net/files/fabric-cdn-prod_20201207.001//assets/brand-icons/product/svg/excel_16x1_5.svg"></i><span class="ms-Button-textContainer textContainer-119"><span class="ms-Button-label label-125" id="id__850">Export</span></span><i data-icon-name="ChevronDown" role="presentation" aria-hidden="true" class="ms-Icon root-33 css-81 ms-Button-menuIcon is-expanded menuIcon-187" style="font-family: FabricMDL2Icons;"></i><span class="ms-layer"></span></span>
CSV button html:
<div class="ms-ContextualMenu-linkContent linkContent-199"><span class="ms-ContextualMenu-itemText label-205">CSV</span></div
Error:
Traceback (most recent call last):
File "C:\Users\main.py", line 44, in <module>
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
File "C:\Users\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
My code:
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pandas
from tkinter import messagebox
driver = webdriver.Chrome(r"C:\Users\chromedriver.exe") # change path
target_url = 'some_url'
driver.get(target_url)
email_field = (By.ID, "i0116")
password_field = (By.ID, "i0118")
next_btn = (By.ID, "idSIButton9")
two_factor_btn = (By.ID, 'idSubmit_SAOTCC_Continue')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(email_field)).send_keys("username")
# Click Next
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# wait for password field and enter password
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(password_field)).send_keys("password")
# Click Login
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# shows message box to promp input
messagebox.showinfo(title='2-step verification', message='Input on 2-step verification, (30 seconds timeout)')
time.sleep(30)
# 2 factor auth
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(two_factor_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(next_btn)).click()
# download csv
excel_btn = (By.ID, 'id__124-menu')
csv_btn = (By.ID, 'ContextualMenu427')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
# driver.find_element_by_xpath('//*[#id="id__124-menu"]/div/ul/li[1]/button/div')
# driver.find_element_by_xpath('//*[#id="ContextualMenu427"]/div/ul/li[2]/button/div/span')
Thanks for your help!
I don't find any such ID the HTML you have provided. Try with following xpath.
excel_btn = (By.XPATH, "//span[text()='Export']")
csv_btn = (By.XPATH, "//Span[text()='CSV']")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(excel_btn)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(csv_btn)).click()
Hope this will resolved your problem.
Related
I was trying to use python selenium to log in to a website. But the login is in a bootstrap modal dialog box, I cannot log in successfully using my code. Could someone please let me know what is wrong with my code?
I have tried the following code:
import requests
import time
import os, sys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import ActionChains
gecko = os.path.normpath(os.path.join(os.path.dirname(__file__), 'geckodriver'))
binary = FirefoxBinary(r'C:/Program Files/Mozilla Firefox/firefox.exe')
driver = webdriver.Firefox(firefox_binary=binary, executable_path=gecko +'.exe')
driver.maximize_window()
wait = WebDriverWait(driver, 10)
time.sleep(1)
pageurl = "https://www.producemarketguide.com"
driver.get(pageurl)
popup = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="close-dialog"]')))
popup.click()
time.sleep(2)
login = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/div[1]/header/div/div[2]/div[2]')))
login.click()
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
user.send_keys('xxx#gmail.com')
password = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[4]/input')))
password.send_keys('XXXX')
submit = wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[6]/button')))
submit.click()
The error message I got is
Traceback (most recent call last):
File "pmg.py", line 34, in <module>
user = wait.until(EC.presence_of_element_located((By.XPATH, '/html/body/app-main/app-widget/screen-layout/main/current-screen/div/screen-login/p[3]')))
File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
That modal dialog is an iframe, so you need to switch to iframe to enter the credentials, try the below code:
accept_cookies = driver.find_element(By.CSS_SELECTOR, "button#onetrust-accept-btn-handler")
if accept_cookies.is_displayed():
accept_cookies.click()
else:
print("Accept Cookies button not displayed")
popup_dialog = driver.find_element(By.ID,"popup-dialog-window")
close_icon = driver.find_element(By.ID, "close-dialog")
if popup_dialog.is_displayed():
close_icon.click()
else:
print("Welcome dialog not displayed")
time.sleep(1)
# Clicking Login/Register link
driver.find_element(By.CSS_SELECTOR, ".user-login.user-login-front.pull-right #user-piano-login-register").click()
time.sleep(1)
# Switch to the login dialog iframe
driver.switch_to.frame(driver.find_element(By.XPATH,".//iframe[starts-with(#id,'piano-id-')]"))
# enter the credentials and click Login button
driver.find_element(By.XPATH,".//input[#fieldloginemail]").send_keys("emailid")
time.sleep(0.5)
driver.find_element(By.XPATH,".//input[#fieldloginpassword]").send_keys("password")
time.sleep(0.5)
driver.find_element(By.XPATH,".//button[#actionlogin]").click()
I am trying to find an element with xpath but it is not working.
import string
import random
import os
from random_word import RandomWords
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://yopmail.com/en/")
driver.find_element(By.NAME, 'login').send_keys('grudsimcobottest')
driver.find_element(By.NAME, 'login').submit()
try:
elem = WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.XPATH, '//div[text()="Confirm account deletion"]'))
)
finally:
a = 1
confirm_email = driver.find_element(By.XPATH, '//div[text()="Confirm account deletion"]')
confirm_email.click
try:
elem = WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.XPATH, '//a[text()="Delete"]'))
)
finally:
a = 1
delete_button_in_email = driver.find_element(By.XPATH, '//a[text()="Delete"]')
delete_button_in_email.click()
This is the code^
Traceback (most recent call last):
File "C:\Users\notgr\Desktop\Everything\SC ahks\python\yopmailmakendelete.py", line 28, in <module>
elem = WebDriverWait(driver, 60).until(
File "C:\Users\notgr\AppData\Local\Programs\Python\Python310\lib\site-packages\selenium\webdriver\support\wait.py", line 90, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
This is the error^
the website
If you go to yopmail.com and type in grudsimcobottest you can see it live if you like. I'm not sure why the div highlighted isn't being selected and clicked, it times out when looking for it. Help much appreciated.
<iframe frameborder="0" width="100%" height="100%" src="" name="ifinbox" onload="onloadifinbox()" id="ifinbox" state="full"></iframe>
Your element is in an iframe switch to it prior to finding the element.
WebDriverWait(driver, 60).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"ifinbox")))
I am trying to automatically login the website using selenium with python. However, I got error as below.
Traceback (most recent call last):
File "C:\Users\KienThong\Automation\Learning\GmailDemo.py", line 15, in <module>
ID = WebDriverWait(driver, 10).until(
File "C:\Python385\lib\site-packages\selenium\webdriver\support\wait.py", line 89, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
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
import time
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.ID, "_xfUid-1-1643985254"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
The reason why you are having an error is that the ID that you are using is dynamic and changes everytime the page is loaded. Therefore we need to use a static identifier which we can with name="login"
driver = webdriver.Chrome()
driver.get("https://www.englishforum.com/study/login/")
driver.maximize_window()
try:
ID = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "login"))
)
ID.send_keys("dkthong2010#gmail.com")
finally:
driver.quit()
you dont need to tell the driver to wait till the element is present your can simply its xpath as it will executed only after page loads
so use:
ID = driver.find_element(By.XPATH, "/html/body/div[2]/div/div[4]/div/div[2]/div/div/div/div/form/div[1]/div/dl[1]/dd/input")
ID.send_keys("dkthong2010#gmail.com")
using xpath to locating the element is very easy and easy to locate.
just
1.inspect the element
2.right click on the element
3. copy full xpath
To send a character sequence to the Your name or email address field you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get('https://www.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[href$='dismiss-notice'] > span.button-text"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='login']"))).send_keys("dkthong2010#gmail.com")
Using XPATH:
driver.get('https://www.englishforum.com/study/login/')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#href, 'dismiss-notice')]/span[#class='button-text']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='login']"))).send_keys("dkthong2010#gmail.com")
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
Browser Snapshot:
I'm trying to click in a button by using CSS Selector. I've tried by using input with value, title and onclick but not working, this is html code:
<div id="botaoMarcar"><input type="button" disabled class="botao"
value="Marcar todas" title="Marcar todas" onclick="javascript:marcarDesmarcarTodos(true);"
></div>
My code:
driver = Chrome()
url = "https://www3.bcb.gov.br/sgspub/localizarseries/localizarSeries.do?method=prepararTelaLocalizarSeries"
driver.get(url)
try:
WebDriverWait(driver, 3).until(EC.alert_is_present(),
'Timed out waiting for PA creation ' + 'confirmation popup to appear.')
alert = driver.switch_to.alert
alert.accept()
except TimeoutException:
print("No Alert")
driver.implicitly_wait(5)
driver.maximize_window()
# This part is for input table code that I want to access
id_code = driver.find_element(By.ID, 'txCodigo')
id_code.send_keys(24)
id_code.send_keys(Keys.ENTER)
# Part not working exactly
clic_code = driver.find_element(By.CSS_SELECTOR, 'input[value*="Marcar todas"]')
clic_code.click()
I just went another way and implemented the function to mark all didn't seem like the element was in an iframe.
time.sleep(5)
driver.execute_script("marcarDesmarcarTodos(true);")
Import:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to enter the postcode into the psotcode field/form box.=
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.currys.co.uk/app/checkout
And this is my current code:
# Selenium Tutorial #1
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
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/samsung-ue75tu7020kxxu-75-smart-4k-ultra-hd-hdr-led-tv-10213562-pdt.html")
# Click "Accept All Cookies" or ignore if no pop up
try:
element = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "onetrust-accept-btn-handler")))
element.click()
except Exception:
pass
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "email-desktop")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.GZkwS")
driver.execute_script("arguments[0].click();", button)
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Continue to Basket"
button = driver.find_element_by_css_selector("button.Button__StyledButton-bvTPUF.hZIOeU.Button-jyKNMA.sc-fzpjYC.gJohPa")
driver.execute_script("arguments[0].click();", button)
# Wait 3 seconds
driver.implicitly_wait(3)
# Click "Go to checkout"
button = driver.find_element_by_xpath("//button[contains(#data-component, 'Button')][contains(#type, 'button')]")
driver.execute_script("arguments[0].click();", button)
# Type in postcode
search=WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//input[#contains='Postcode Checker")))
search.send_keys("NG229NU")
search.send_keys(Keys.RETURN)
your locator is wrong for search , use:
search = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//input[#aria-label="Postcode Checker"]')))