I am working on web scraping task using selenium and stuck at click function.
Steps in Website:
1. Open Website
2. Enter Key value in the search text box
3. Click on Search to start the search process
After step 3 it is supposed to load the progress bar and start searching for results.
However, after clicking on search, Progress Bar appears for a second and goes away.
My code:
browser = webdriver.Chrome(executable_path='C:/Chrome/chromedriver.exe')
browser.set_page_load_timeout(30000)
browser.get("labs.nccgroup.trust/typofinder/")
browser.find_element_by_id('host').send_keys("example.com")
elem=browser.find_element_by_xpath("//*[#id='typogulator']/input[2]")
elem.click()
Try Use WebDriverWait after inserting value in search field.
from selenium import webdriver
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('https://labs.nccgroup.trust/typofinder')
browser.find_element_by_id('host').send_keys("example.com")
ele=WebDriverWait(browser,20).until(EC.element_to_be_clickable((By.XPATH,"//input[#type='submit'][#value='Search']")))
ele.click()
Related
Im trying to create automation for a cookie clicker website.
I need to click on elements (like the cursor element for example) on the website when they go from "blocked" to "unlocked" I have been trying for 2 days now and I have tried using the WebDriverWait but nothing is working no matter what my code does not detect when the element becomes available.
this is my code right now
import time
import ec as ec
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
Play = True
ser_obj = Service("\Progr\OneDrive\Documents\PythonFolder\chromedriver.exe")
driver = webdriver.Chrome(service=ser_obj)
driver.get(url="https://orteil.dashnet.org/cookieclicker/")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.cc_btn.cc_btn_accept_all"))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.langSelectButton.title#langSelect-EN"))).click()
time.sleep(1)
Cookie = driver.find_element(By.CSS_SELECTOR, "#cookieAnchor #bigCookie")
while Play:
Cookie.click()
Cookie_number = (driver.find_element(By.XPATH,'//*[#id="cookies"]').text)
print(Cookie_number)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, '//*[#id="product0"]'))).click()
and for whatever reason I cannot click on the cookie unless I have a time.sleep() method called and I do not know why. I have tried using WebDriverWait to wait when the cookie becomes avaible to click, but nope, it wont run without the time.sleep().
Any help would be great.
I have tried using if statments with the .isDisplayed() function.
I have tried using "try-except" methods.
I have tried giving Play a value and then saying when that value reaches 0, check to see if the cursor is available to click.
I have tried using CSS Selectors and Xpath
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 am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to click the "Checkout" button using "element_to_be_clickable((By.XPATH".
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket
And this is my current code for the clicking:
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.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
checkout = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH("//img[contains(#src,'/static/images/checkoutv2.png.png')]"))).click()
I can see your xpath is not correct.
Your Xpath should be.
//img[contains(#src,'/static/images/checkoutv2.png')]
Your code should be.
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
The link you provided contains hCaptcha, which is actually responsible to check whether you are a human being or a bot. I guess that it's also the reason, why you can't click any of the items on the page, because Selenium actually is nothing less than a bot.
You first have to pass the test by clicking on the images, which are asked for.
After the website is loaded I click a button successfully which will then generate some numbers in this class
<div class="styles__Value-sc-1bfbyy7-2 eVmhyz"></div>
but not instantly, it will put them in one by one. Selenium will instantly grab the first value that gets put into the class but doesn't wait for the other values to get added. Any way to wait for it to load all the values in there before grabbing it.
Here is the python code I use for grabbing the value:
total = driver.find_element_by_xpath("//div[#class='styles__Value-sc-1bfbyy7-2 eVmhyz']").text
Selenium has a WebDriverWait method:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
browser = webdriver.Chrome()
delay = 5
total = WebDriverWait(browser, delay).until(expected_conditions.presence_of_element_located(<locator>)
I haven't tested it locally but it may work. There is also presence_of_all_elements_located method, you can find the details on this page.
Hope this helps!
I'm trying to click on a "VIEW" button from the table shown below in the image from this site using Selenium Python.
I tried using xpath, name and all. How to create a loop for this?
All the entries is to be left empty just click on search button a table is displayed from there "VIEW" button is to be selected
Code:
Check below lines , and change the path of driver
from selenium import webdriver
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
opt = webdriver.ChromeOptions()
opt.add_argument("--ignore-certificate-errors")
opt.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path="C:\\chrome driver\\chromedriver.exe", options=opt)
driver.get("http://rera.rajasthan.gov.in/ProjectSearch")
search_btn = driver.find_element_by_xpath('//*[#id="btn_SearchProjectSubmit"]')
# invoke the click() action
search_btn.click()
xpath_first_view = '//*[#id="OuterProjectGrid"]/div[3]/div[2]/div/table/tbody/tr[1]/td[7]'
# wait for the element to ensure it is available
WebDriverWait(driver,30).until(EC.element_to_be_clickable((By.XPATH,xpath_first_view)))
time.sleep(1) # to ensure click is not very quick
view1_btn = driver.find_element_by_xpath(xpath_first_view).click()