Python selenium how to select something from a dropdown box - python

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!

Related

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

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:

TypeError: 'FirefoxWebElement' object is not iterable [duplicate]

This question already has answers here:
TypeError: 'WebElement' object is not iterable error
(2 answers)
Closed 4 years ago.
I would like to get URLs of Airbnb's listing pages by Python, selenium, firefox, however, my program doesn't work well.
My error code is as bellow;
Original exception was:
Traceback (most recent call last):
File "pages.py", line 19, in <module>
for links in driver.find_element_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]'):
TypeError: 'FirefoxWebElement' object is not iterable
Here is my code!
from selenium import webdriver
from selenium.webdriver import FirefoxOptions
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.common.exceptions import TimeoutException
test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7'
opts = FirefoxOptions()
opts.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=opts)
driver.get(test_url)
driver.implicitly_wait(30)
for links in driver.find_element_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]'):
listing_url = links.get_attribute('href')
print(listing_url)
driver.quit()
I tried to change my code, another code is as bellow;
(Error message is same as my first code.)
from selenium import webdriver
from selenium.webdriver import FirefoxOptions
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.common.exceptions import TimeoutException
test_url = 'https://www.airbnb.jp/s/%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C/homes?refinement_paths%5B%5D=%2Fhomes&query=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&price_min=15000&allow_override%5B%5D=&checkin=2018-07-07&checkout=2018-07-08&place_id=ChIJ51ur7mJw9TQR79H9hnJhuzU&s_tag=z4scstF7'
opts = FirefoxOptions()
opts.add_argument("--headless")
driver = webdriver.Firefox(firefox_options=opts)
driver.get(test_url)
driver.implicitly_wait(30)
links = driver.find_element_by_xpath('//a[contains(#href, "rooms")]')
for link in links:
listing_url = link.get_attribute('href')
print(listing_url)
driver.quit()
I am glad to you reply if you have a time.
Thank you.
You need to use find_elements_by_xpath where return a list of elements
Not find_element_by_xpath that returned only one element
...
links = driver.find_elements_by_xpath('//div[contains(#id, "listing-")]//a[contains(#href, "rooms")]')
for link in links:
print(link.get_attribute('href')
...
Output
https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08
https://www.airbnb.jp/rooms/7793811?location=%E6%97%A5%E6%9C%AC%E6%B2%96%E7%B8%84%E7%9C%8C&check_in=2018-07-07&check_out=2018-07-08
...

WebDriverWait + Dropdown size menu

I am attempting to make a auto script bot that find a specific item and adds the item to the users cart and so forth. Right now I am caught up getting python to select the size from the dropdown menu.
I also went with the WebDriverWait function because it was giving me a element not found error , so i assumed it the 'size' element had not loaded yet.
Python is also throwing me this error
"TypeError: 'str' object is not callable"
Below is also a picture of the html code I am referencing to pull the information from. Also will appreciate any advice on better executions.
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
# Open chrome web browswer and directs to supreme.com
browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')
#Find specific item
browser.find_element_by_xpath('//*
[#id="container"]/article[14]/div/a/img').click()
#Wait for element to load
pause = WebDriverWait(browser,10).until(
EC.visibility_of_any_elements_located(By.ID('size'))
)
# Select size
Select = Select(browser.find_element_by_id('size'))
Select.select_by_visible_text("Large")
Try below code.
from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
browser.get('http://www.supremenewyork.com/shop/all')
browser.find_element_by_xpath('//*[#id="container"]/article[12]/div/a/img').click()
WebDriverWait(browser,10).until(EC.visibility_of_any_elements_located((By.ID,'size')))
select = Select(browser.find_element_by_id('size'))
select.select_by_visible_text("Medium")

Python/Selenium im getting error and can not fix it

here is my code
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
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
from selenium.webdriver.common.keys import Keys
import random
results = []
with open("al.txt") as inputfile:
for line in inputfile:
results.extend(line.strip().split(','))
x = random.choice(results)
driver = webdriver.Firefox()
while True:
driver.get(x)
element = driver.find_element_by_id("vB_Editor_QR_textarea")
driver.implicitly_wait(10)
element.send_keys("example")
driver.implicitly_wait(60)
driver.close()
i get
IndentationError: unexpected indent
PS C:\Users\asus\temp> python clickex1.py
File "clickex1.py", line 24
driver.implicitly_wait(60)
is it possible that selenium is installed wrong so python doesn't read the library?
the other lines are working perfectly fine. i need some help with this im new in using selenium and also python,
the script needs to go to the random websites in al.txt and post something in the designated area

Categories

Resources