selenium. x path for data in carousel - python

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

Related

Python selenium how to select something from a dropdown box

I am trying to make a quick bot to get kumon answers and it logs in just fine in able to get anything you need to select something from a dropdown box. This is what I have
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.support.ui import Select
from selenium.webdriver.support.select import Select
import time
PATH = "/usr/local/bin/chromedriver"
driver = webdriver.Chrome(PATH)
driver.get("https://www.kumon.com/AnswerBook/AnswerBookLogin.aspx")
acc = driver.find_element_by_id("txtStudentID")
name = driver.find_element_by_id("txtPassword")
#select = driver.find_element_by_name("drpLevel")
acc.send_keys("id")
name.send_keys("pass")
name.send_keys(Keys.RETURN)
#time.sleep(1)
driver.implicitly_wait(4)
dropA = driver.find_element_by_id("drpLevel")
dropA.select_by_visible_text('G')
but it gives me this error:
Traceback (most recent call last):
File "/xxx/xxx/xxx/xxx/xxx", line 25, in <module>
dropA.select_by_visible_text('G')
AttributeError: 'WebElement' object has no attribute 'select_by_visible_text'
The html code is
I don't know what is causing this but if anyone can help me thanks!

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:

Error "name 'by' is not defined" using Python Selenium WebDriver

I keep getting an error as below:
NameError: name 'By' is not defined
for the code
chrome_driver_path = r"C:\chromedriver.exe"
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
browser = webdriver.Chrome(chrome_driver_path)
browser.delete_all_cookies()
browser.get("https://www.google.com/")
wait = WebDriverWait(browser, 10)
element = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="tsf"]/div[2]/div[3]/center/input[1]')))
Is my import correct?
You have to import it:
from selenium.webdriver.common.by import By
You can import By by using:
from selenium.webdriver.common.by import By
Adding this line at the top of the code resolved my problem:
from selenium.webdriver.common.by import By

Can't set a checkbox with selenium-webdriver

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.

Finding element with explicit wait using selenium webdriver in python

I am trying to find the element with the link text, I am using following code
handle = driver.window_handles
#handle for windows
driver.switch_to.window(handle[1])
#switching to new window
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
And I am getting following traceback
Traceback (most recent call last):
File "<pyshell#28>", line 1, in <module>
link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Followers ")))
File "C:\Python27\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
raise TimeoutException(message)
TimeoutException: Message: ''
HTML of the element I am trying to select is
Followers <span class="profile_count">43,799</span>
How can I solve this problem??
If you use By.LINK_TEXT, there should be a link with exactly that text: Followers, but you have Followers 43,799.
In your case, you should use By.PARTIAL_LINK_TEXT instead:
wait.until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, 'Followers')))
UPDATE Here's working example:
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() # CHANGEME
driver.get('http://www.quora.com/Kevin-Rose')
element = WebDriverWait(driver, 2).until(
EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))
)
element.click()
Cheers
from selenium import webdrivercode
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("http://somedomain/url_that_delays_loading")
#Follow below syntax
element = WebDriverWait(driver, 10)
element.until(EC.presence_of_element_located((By.ID, '--webElement--')))

Categories

Resources