I am new to selenium and I try to navigate on a page and to click on a button to get to the next page with selenium web driver.
This is my python code:
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
WebDriverWait(driver, 30).until(EC.presence_of_element_located((By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]')))
driver.find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]').send_keys(Keys.RETURN)
print('Element found')
In addition to send_keys(Keys.RETURN) I also tried send_keys(Keys.ENTER) and click(). In all cases the statement "Element found" is printed to the console, but nothing happens on the webpage.
Here is the HTML bit of the button:
<span class="ACME-src-ACME-ui-Pagination--arrowRight">
<i class="flaticon-right-arrow"></I>
</span>
Every help is highly appreciated.
Wait!! Looks like The Xpath you are using is incorrect. Spans are not clickable objects. Or like they are clickable but do not do anything, which is exactly happening here. May be the "i" tag is something which should take click(However I cannot see any object in Dom taking click and performing a job)
First provide me with the proper DOM.
You can further try two more ways of doing it:-
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
driver.execute_script("arguments[0].click();", button)
OR
button = find_element(By.XPATH, '//*[#id="page"]/div/div/div[5]/div/div/div/div[3]/div/span[2]/i')
ActionChains(driver).move_to_element(button).click().perform()
Related
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.
I want to go to this site:
http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia
Then click on the "Tabella" tab
Then see the second page (out of 64) of the table
However I failed in the first part, I cannot make a code to click on "Tabella" tab
import requests
from selenium import webdriver
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
element = driver.find_element_by_id("Tabella")
element.click()
And here is the html code that I used to search:
<li id="Tabella" class="Table_img ui-state-default ui-corner-top ui-tabs-selected ui-state-active">
<span id="span_table_img" class="span_img"></span>
Tabella </li>
Thank all!
Add time sleep before accessing the table element.
time.sleep(5)
driver.find_element_by_id('Tabella').click()
To click on Tabella tab you need to induce WebDriverWait() and wait for element_to_be_clickable() and you can use following locator.
Link Text:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.LINK_TEXT,"Tabella"))).click()
XPATH:
driver.get("http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia")
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Tabella']"))).click()
You need to import below libraries.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Problem with your code
You are trying to click an element before it is available/ready to click in DOM.
Solution
You need to wait for the element to load and become clickable to perform any kind of action on the same element.
Code
driver = webdriver.Chrome()
driver.get('http://www.datiopen.it/it/opendata/Mappa_delle_stazioni_ferroviarie_in_Italia')
def wait_for_element_to_be_clickable(element):
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, element)))
wait_for_element_to_be_clickable("Tabella")
element = driver.find_element_by_id("Tabella")
element.click()
print("executed")
def wait_for_element_to_be_clickable(element): this method will wait for any element to become clickable before proceeding. (you can increase the time based on your page loading time).
I am a learning python right now and try to finalize my second script.
I know how to click on an element by Id, text and ngclick from selenium - but none of these worked for my current one so far :/
This element is completely new to me - I am trying to click on this button:
<button type="submit" class="btn btn-primary">Submit</button>
Found something in the previous questions which is
driver.find_element_by_css_selector("btn").click()
but have no idea what driver is referring to :-)
Can somebody help a drowning beginner?^^
Best
Vanassins
Steps:
Create a WebDriver instance.
Navigate to a Web page.
Locate an HTML element from UI.
Perform an action on an HTML element.
Run tests and record test results using a test framework.
Sample 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.Firefox()
driver.get("http://www.google.com")
input_element = driver.find_element_by_name("q")
input_element.send_keys("Test Selenium")
input_element.submit()
I am working with this code code:
I am trying to find text "Branch Selection" within a web page to click on it.
So I do this:
driver.find_elements_by_xpath("//*[contains(text(), 'Branch Selection')]").click()
It doesn't throw an error, but the click doesn't happen. What am I doing wrong?
If it clicks but nothing happens, first try adding a sleep like sleep(5) to help debug before clicking to see if its because Selenium thought the page loaded when it actually didn't finish loading. If you can click after sleep of 5 seconds then you need to use WebDriverWait and EC as #DebanjanB had shown. In worst case scenario you'll have to use a sleep in your code but try to get the sleep to as short as possible.
Otherwise you might have multiple elements on the page with Branch Selection text such as in the meta tags. Try using XPATH example below to isolate the XPATH look up:
"//button[.//*[contains(text(), 'Branch Selection')]]"
or if there's more than one phrase on page containing the text, use following to select exact text
"//button[.//*[text()='Branch Selection']"
This selects the button element that has a child element with the text you're looking for. More XPATH details here: https://devhints.io/xpath
As per the HTML provided to click on the element with text as Branch Selection you need to induce WebDriverWait for the element to be clickable as follows :
CSS_SELECTOR :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.item.item-block.item-md div.input-wrapper>ion-label.label.label-md[id^=lbl-]"))).click()
XPATH :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='item item-block item-md']//div[#class='input-wrapper']/ion-label[#class='label label-md' and starts-with(#id,'lbl-') and contains(.,'Branch Selection')]"))).click()