Can't set a checkbox with selenium-webdriver - python

I'm testing my app with tumblr and I have to log in and out as I go through procedures. While doing so, I'm having trouble clicking a checkbox that keeps popping up. How can I use selenium-webriver in python to click it?
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import sys
import smtplib
email = "xxx#hotmail.com"
pswd = "xxxxx"
driver = webdriver.Firefox()
actions = ActionChains(driver)
driver.get("https://www.tumblr.com/login")
driver.find_element_by_id("signup_email").send_keys(email)
driver.find_element_by_id("signup_password").send_keys(pswd)
driver.find_element_by_id("signup_forms_submit").click()
#wait = WebDriverWait(driver, 5)
time.sleep(5)
try:
#checkbox = driver.find_element_by_id("recaptcha-anchor")
#checkbox = driver.find_element_by_id("g-recaptcha")
#checkbox.click()
box = driver.find_element_by_xpath("//*[#id='recaptcha-token']")
#box = driver.find_element_by_css_selector("#recaptcha-anchor")
print(box.location, box.size)
box.click()
#actions.move_to_element(box)
actions.click(box)
#actions.perform()
except NoSuchElementException as e:
print(e)
pass
(EDIT)
My error reads:
Traceback (most recent call last):
File "tumblrtest.py", line 49, in <module>
EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 76, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///var/folders/13/1rh6kf9x2k11pyfg6zsnfmg40000gn/T/tmpvkFkz_/extensions/fxdriver#googlecode.com/components/driver-component.js:10667)
at FirefoxDriver.prototype.findElement (file:///var/folders/13/1rh6kf9x2k11pyfg6zsnfmg40000gn/T/tmpvkFkz_/extensions/fxdriver#googlecode.com/components/driver-component.js:10676)
at DelayedCommand.prototype.executeInternal_/h (file:///var/folders/13/1rh6kf9x2k11pyfg6zsnfmg40000gn/T/tmpvkFkz_/extensions/fxdriver#googlecode.com/components/command-processor.js:12643)
at DelayedCommand.prototype.executeInternal_ (file:///var/folders/13/1rh6kf9x2k11pyfg6zsnfmg40000gn/T/tmpvkFkz_/extensions/fxdriver#googlecode.com/components/command-processor.js:12648)
at DelayedCommand.prototype.execute/< (file:///var/folders/13/1rh6kf9x2k11pyfg6zsnfmg40000gn/T/tmpvkFkz_/extensions/fxdriver#googlecode.com/components/command-processor.js:12590)
This was my error in Chrome: Traceback (most recent call last):
File "tumblrtest.py", line 49, in <module>
EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))
File "/Library/Python/2.7/site-packages/selenium/webdriver/support/wait.py", line 76, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
...nothing was clicked. :\

Click the recaptcha-anchor instead:
driver.find_element_by_id("recaptcha-anchor").click()
You might also need to wait for the element to be clickable before performing an action:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
recaptcha_anchor = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.ID, "recaptcha-anchor"))
)
recaptcha_anchor.click()

Find recaptcha checbox with
recaptcha = self.driver.find_element_by_xpath("//*[#role='presentation']"); time.sleep(random.uniform(2, 5))
then click it
recaptcha.click(); time.sleep(random.uniform(1, 1))
This method is currently working.

Related

Python selenium timeout exception without message when clicking

I want to search specific word in ScienceDirect and when is shows results I want to click 100 result per page at the bottom on page.
HTML code:
<a class="anchor" data-aa-region="srp-pagination-options" data-aa-name="srp-100-results-per-page" href="/search?qs=Python&show=100"><span class="anchor-text">100</span></a>
And that's my code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome()
driver.get("https://www.sciencedirect.com/")
assert "Science" in driver.title
elem = driver.find_element(By.ID, "qs-searchbox-input")
elem.clear()
elem.send_keys("Python")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
element = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, ".data-aa-name[value='srp-100-results-per-page']"))
)
element.click()
driver.close()
And exception:
Traceback (most recent call last):
File "X:\pythonProject\selenium\count_cited.py", line 15, in <module>
element = WebDriverWait(driver, 10).until(
File "X:\pythonProject\selenium\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 95, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Use for example this CSS selector:
"div#srp-pagination-options li:nth-child(3)"

How to Accept Cookies alert with Selenium in Python

I'm trying to get to google.com and type something in the search bar. But the cookies alert pop-up always gets in my way. So I have to click the button 'I agree'. I know I have to wait a little bit of time before searching for the element but even if I'll wait with WebDriverWait() function, or .implicitly_wait() it just doesn't want to find the element (I used the search by xpath and .click() to press the button).
Spent hours trying to find a solution...
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
def main():
driver = webdriver.Chrome('chromedriver.exe')
url = 'https://www.google.com/'
driver.get(url)
# The following line is supposed to wait for the button to appear and then click:
agreeButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='introAgreeButton']")))
agreeButton.click()
main()
Also i should include the error here:
Traceback (most recent call last):
File "{SCRIPT PATH}", line 49, in <module>
main()
File "{SCRIPT PATH}", line 46, in main
agreeButton = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//*[#id='introAgreeButton']")))
File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

selenium. x path for data in carousel

I expect the script below to print a bunch of car names
I think I'm not pointing to the carousel properly.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import time
driver = webdriver.Chrome(executable_path=r"../Downloads/chromedriver.exe")
driver.get('https://www.nationwidevehiclecontracts.co.uk/?msclkid=22db17e69fc512c5ab9f49993f841304&utm_source=bing&utm_medium=cpc&utm_campaign=B%20-%20NVC%20Brand%20-%20(Exact)&utm_term=nationwide%20car%20leasing&utm_content=Brand%20Variations')
carousel_x_path="/section[#class='section section--widget section--full-carousel section__heading-scroll variant-display']"
WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,carousel_x_path)));
for name in (driver.find_elements_by_xpath(carousel_x_path+"/span[#class='make']")):
print("it found a name")
print(name.get_attribute("textContent"))
I get this error.
Traceback (most recent call last):
File "C:\Users\User\Desktop\miniiiiii.py", line 10, in <module>
WebDriverWait(driver,30).until(EC.visibility_of_element_located((By.XPATH,carousel_x_path)));
File "C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
can you please tell me whats wrong
Please check below solution
driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.get('https://www.nationwidevehiclecontracts.co.uk/?msclkid=22db17e69fc512c5ab9f49993f841304&utm_source=bing&utm_medium=cpc&utm_campaign=B%20-%20NVC%20Brand%20-%20(Exact)&utm_term=nationwide%20car%20leasing&utm_content=Brand%20Variations')
driver.maximize_window()
WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//body[#name='top']/section[#id='hotdeals']")))
links = driver.find_elements_by_xpath("//body[#name='top']/section[#id='hotdeals']/div[#id='offer-carousel']/div[#class='slick-list draggable']//h3//a")
for name in links:
print name.text
Note: please add below import to your solution
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

How to Access span element and change it's value

I am trying to create a python + selenium script in order to fetch CSV from a salesforce pardot page.
Can Somebody please help me in accessing the nested span element inside the dropdown list which will be generated on button click.
I am adding my code done so far and I am getting Timeout Error While running my script.
from selenium import webdriver
import time
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 import webdriver
try:
chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory': r'C:\Pardot'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path="D:\XXX XXXX\XXXX\drivers\chromedriver.exe", options=chrome_options)
driver.get('https://pi.pardot.com/engagementStudio/studio#/15627/reporting')
user_name = driver.find_element_by_css_selector('#email_address')
user_name.send_keys('XXXXXXXXXXXXXXXXXXX')
password = driver.find_element_by_css_selector('#password')
password.send_keys('XXXXXXXXXXXXXXXXX)
submit_button = driver.find_element_by_css_selector('input.btn')
submit_button.click()
WebDriverWait(driver, 10).until(
EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe#content-frame")))
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(text_='All Time',locator=(By.CSS_SELECTOR,"span[data-qa='reporting-filter-trigger-value']")))
except (RuntimeError) as e:
print(e)
finally:
time.sleep(10)
driver.close()
Screenshot for span DOM element
Error Stack trace:
C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\python.exe "D:/Vipul Garg Backup/XXXX/testingCSVfetching.py"
Traceback (most recent call last):
File "D:/Vipul Garg Backup/XXXX/testingCSVfetching.py", line 22, in <module>
WebDriverWait(driver, 10).until(EC.text_to_be_present_in_element_value(text_='All Time',locator=(By.CSS_SELECTOR,"span[data-qa='reporting-filter-trigger-value']")))
File "C:\Users\Vipul.Garg\AppData\Local\Programs\Python\Python37\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
try this approach
spans = driver.find_element_by_css_selector('.slds-button_reset.slds-grow.slds-has-blur-focus.trigger-button').get_attribute('innerHTML')
print(spans)
You should be getting all span.

selenium python wait time time out exception when it used to work before

I have code that is supposed to log into etsy after a certain amount of time. This code has not had any changes and used to work perfectly on an old machine that I had. Now however when I run this on another windows 10 machine, I get a time out exception.
Code:
import selenium
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from datetime import datetime
import time
from time import sleep
from selenium.webdriver.common.action_chains import ActionChains
browser = webdriver.Chrome()
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
browser.find_element_by_id("sign-in").click()
wait = WebDriverWait(browser, 10)
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
username = browser.find_element_by_id("username-existing")
password = browser.find_element_by_id("password-existing")
username.click()
username = browser.find_element_by_id("username-existing")
It gets to the wait.until line then it times out with this message:
> Navigating to website...
Traceback (most recent call last):
File "EstyBot.py", line 183, in <module>
main()
File "EstyBot.py", line 180, in main
get_item(item)
File "EstyBot.py", line 61, in get_item
wait.until(EC.element_to_be_clickable((By.ID,"username-existing")))
File "C:\Users\user\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\support\wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
I have tried using sleep, but then I get the error that the element is not visible.
I have tried by XPATH and tried EC as element to be visible to only hit the same error message as below.
Seems the desired locators have changed and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
browser.get("https://www.etsy.com/shop/FidoandFifi?ref=l2-shopheader-name") #navigates to hoshiikins.com
print("Navigating to website...")
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#sign-in"))).click()
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input.input.input-large#join_neu_email_field"))).send_keys("Taryn#Antoinette.Raines")
browser.find_element_by_css_selector("input.input.input-large#join_neu_password_field").send_keys("Taryn_Antoinette_Raines")
Browser Snapshot:

Categories

Resources