I wish to create a bot for automatically fill in Google Form survey, which is filled with radio buttons. When I am trying to fill in the form, the first Gender radio button was successfully clicked. However, the second age group button was not? Is this a bug?
import time
from selenium import webdriver
browser = webdriver.Chrome(executable_path='C:/Users/ryan/Downloads/chromedriver.exe')
browser.get('https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBB/viewform?fbzx=-1760864547538697754')
RadioGender= browser.find_element_by_xpath('//*[#id="i5"]/div[3]/div')
RadioGender.click()
RadioAge= browser.find_element_by_xpath('//*[#id="i18"]/div[3]/div/div')
RadioAge.click()
To handle dynamic element use WebDriverWait() and wait for element to be clickable and following locator strategy.
browser.get("https://docs.google.com/forms/d/e/1FAIpQLScA8I-O5FQoYPwU6yoYJmAwrIJdBBt1MqEgoQOz9oHWWmTY1Q/viewform?fbzx=-1760864547538697754")
WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//span[text()="Next"]'))).click()
RadioGender= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#data-value="Male"]')))
RadioGender.click()
RadioAge= WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#data-value="21-23"]')))
RadioAge.click()
You need to import following libraries.
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
Note: If you would like to select other age range, you need pass those values as data value.
Related
I currently wish to select a value from a drop-down menu in python with selenium.
The site is the following: https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M
My current 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
import time
driver = webdriver.Safari(executable_path = '/usr/bin/safaridriver')
wait = WebDriverWait(driver, 20)
driver.get("https://www.palyazat.gov.hu/tamogatott_projektkereso?fbclid=IwAR3rmPVj-YAVoMTs2Vodj7JKTVIAZkbTiZ9z4b0j04mq2ThECw5kQOI1p7M")
wait.until(EC.visibility_of_element_located((By.XPATH, "//div[#class='form-group' and .//label[#for='programok']]//div[contains(#class,'css-1wy0on6')]"))).click()
time.sleep(1)
current_program = wait.until(EC.visibility_of_element_located((By.XPATH, "//div[contains(#class,'css-26l3qy-menu')]")))
current_program.click()
My issue is that I am not able to find out the needed xpath code for the pop-upped drop-down menu, since I can see it in developer view only if the list is clicked, but I can see then only that "//div[contains(#class,'css-26l3qy-menu')]" is a suitable xpath for the menu. (see the enclosed picture)
But it chooses a random value. How specify which value I want to select?
I am facing interesting issue with my **Web Scraping** use case. I need to get newest **Google Maps reviews**. I want to sort reviews by latest date. And all tutorials I am watching is in English, but in my native language the UI is different than in those tutorials.
I am able to click on the button using **Selenium** and button's **XPATH**, but I don't know how to change the sorting options from the visible drop menu.
# Click the sort button
driver.find_element_by_xpath('//*[#id="pane"]/div/div[1]/div/div/div[2]/div[8]/button').click()
select_by_visible_text() and select_by_value() doesn't work for me as I cannot select the button and doesn't work on div.
URL I am using : Link
To see my UI change to LITHUANIAN language.
First of all you have to learn how to create correct XPath locators.
Long XPath expressions are too fragile.
The "Sort Reviews" button locator instead of
//*[#id="pane"]/div/div[1]/div/div/div[2]/div[8]/button can be
//button[#aria-label='Sort reviews'] or
//button[#data-value='Sort']
After clicking this button, to sort reviews by latest date you can click this element: //li[#data-index='1']
So basically this will work:
driver.find_element_by_xpath("//li[#data-index='1']").click()
But since you need to wait for dialog to open after clicking the Sort button you need to utilize Expected Condition wait, as following:
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.XPATH, "//li[#data-index='1']"))).click()
You will need the following imports for this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
This code should work.
I added a webdriverwait after clicking on the 'Sort' button to wait until the all the options in the dropdown are visible, then clicked on 'Highest rating'.
//li[#role='menuitemradio'])[3] refers to the 3rd element in the dropdown which is the 'Higest rating'. I tried using the text to be specific and not count on element index, but somehow it is not working. But the below code does sort the reviews.
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 webdriver_manager.chrome import ChromeDriverManager
import time
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.maximize_window()
driver.get("https://www.google.com/maps/place/Senukai/#54.6832836,25.183965,12z/data=!4m11!1m2!2m1!1svilnius+senukai!3m7!1s0x46dd94055529fabf:0xb1132b0ad981d43b!8m2!3d54.7098368!4d25.2999662!9m1!1b1!15sCg92aWxuaXVzIHNlbnVrYWkiA4gBAVoRIg92aWxuaXVzIHNlbnVrYWmSARhidWlsZGluZ19tYXRlcmlhbHNfc3RvcmU")
print(driver.title)
time.sleep(5)
driver.find_element(By.XPATH, "//button[#data-value='Sort']").click()
WebDriverWait(driver, 10).until(EC.visibility_of_all_elements_located((By.XPATH, "//li[#role='menuitemradio']")))
driver.find_element(By.XPATH, "(//li[#role='menuitemradio'])[3]").click()
time.sleep(2)
driver.quit()
P.S I used time.sleep occasionally to make a quick code, but it would be a good practice to use WebdriverWait in lieu of time.sleep
I'm trying to create an automation test in Asos (for practice purpose only) however I'm having a hard time locating this sign-in element...
I need to click on that sign-in button.
these are the element I got in inspect:
a class="_1336dMe _1uUU2Co _1336dMe _1uUU2Co" href="https://my.asos.com/my-account?
lang=en-GB&store=COM&country=GB&keyStoreDataversion=3pmn72e-27"
data-testid="signin-link" tabindex="-1">Sign In
I had the same problem when trying to find this button on Google Maps. Is the sign in button on a pop up window? Then the problem is becuse you have to change between frames.
Here is a code sample:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('driver path')
url = 'url'
driver.get(url)
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//*[#id="consent-bump"]/div/div[1]/iframe')))
agree = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="introAgreeButton"]/span/span')))
agree.click()
#back to the main page
driver.switch_to_default_content()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchboxinput"]'))).send_keys('gostilne')
search = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="searchbox-searchbutton"]')))
search.click()
Make shure that the xpath for frames and buttons is correct.
So, I've decided to make TikTok massfollower, but can't go further than login.
from selenium.webdriver import Firefox
from time import sleep
browser = Firefox()
browser.get('https://www.tiktok.com/foryou?lang=ru')
login = browser.find_element_by_class_name('jsx-3665539393')
login.click()
sleep(10)
login2 = browser.find_element_by_class_name('channel-name-2Dwny')
login2.click()
It goes to TikTok website, then it presses the log-in button, and then there is pop-up window(I hope that's the right way to call it). But program can't find elements on this pop-up window, I thought that I should wait for pop-up to load, but there is no difference.
The element you are after is inside an iframe you need to switch to iframe first in order to access the element.
Induce WebDriverWait() and wait for frame_to_be_available_and_switch_to_it() and following css selector.
Induce WebDriverWait() and wait for element_to_be_clickable()
browser.get('https://www.tiktok.com/foryou?lang=ru')
#Click on Login
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"a.jsx-3665539393"))).click()
#Click cookeis button
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.button-wrapper>button"))).click()
WebDriverWait(browser,10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[src*='tiktok']")))
browser.execute_script("arguments[0].click();",WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div.close-modal-8dfIo"))))
You need to import following libraries
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
I am trying to switch into a new frame, which opens after clicking a button. Unfortunately, I receive a None value.
driver = webdriver.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
time.sleep(5)
driver.find_element_by_xpath('//button[#id="pagAnuShowContactForm"]').click()
time.sleep(5)
Till here, it works to open the contact information. Here, I would like to perform actions in the new, opened window.
I tried the following options:
1.
contact = driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
2.
contact = driver.find_element_by_xpath('//div[#class="telefonos"]')
To get the telephone number.The element is present inside iframe ID ifrw you need to switch to iframe first.
Induce WebDriverWait And frame_to_be_available_and_switch_to_it()
Induce WebDriverWait And visibility_of_element_located()
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.Firefox()
driver.get('https://www.milanuncios.com/dacia-de-segunda-mano/dacia-sandero-1-5-dci-exportacion-323650137.htm')
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//button[#id='pagAnuShowContactForm']"))).click()
WebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.ID,"ifrw")))
print(WebDriverWait(driver,10).until(EC.visibility_of_element_located((By.CSS_SELECTOR,".telefonos"))).text.strip())
driver.close()
Output:
663473583