Click on a button search using classes with -selenium -python - python

I try clicking on the button search but this is not work.
Here is the top code :
driver = webdriver.Chrome()
url = "https://www.flashscore.fr/"
driver.get(url)
the code that problem (first version):
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search")))
driver.execute_script("arguments[0].click();", buttonSearch)
This code does not what is asked but does not display an error.
So I have chosen a class in the class header__button--search named searchIcon___2C7g3ZD
Here it is:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
driver.execute_script("arguments[0].click();", buttonSearch)
It display this error : selenium.common.exceptions.JavascriptException: Message: javascript error: arguments[0].click is not a function
Why is it not possible of click on the button search from page ?
EDIT :
here is code HTML of button search on the page :
<div id="search-window" class="header__button header__button--search"><span class="searchIcon___HyESXZA"><svg class="searchIcon___2C7g3ZD "><title>Search</title><use xlink:href="/res/_fs/build/control-icons-symbols.0acb5c0.svg#icon--search"></use></svg></span></div>

buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__button--search"))) driver.execute_script("arguments[0].click();", buttonSearch)
This does not work because you didn't call any method on buttonSearch. For example methods such as buttonSearch.click(), or buttonsearch.doubleClick(), etc. Without calling a method, the error will not be displayed. This is because instead of using the css selector of the element you instead used it's class name.
You then noticed that and amended that issue like so:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD")))
driver.execute_script("arguments[0].click();", buttonSearch)
However, that allowed the element to be located. But the webdriver is now unable to interact with the element. Because in the error it is explicitly stated that arguments[0].click is not a function.
So to fix this, instead of of using execute_script simply use click() instead. It's a simpler way, and the intended way. Like this:
buttonSearch = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".searchIcon___2C7g3ZD"))).click()

Related

IF CONDITION doesn't work between Recaptcha and another Element, just 1 works. Selenium python

I am trying to do if-else condition for Selenium Python with Recaptcha.
I'm checking a website and sometimes a Recaptcha appears to solve.
and sometimes it doesn't appear and the submit button can be clicked.
I want the code to switch to Recaptcha and solve if it appears and click the submit button if not.
my tries
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
driver.find_element(By.ID,"btnSSSubmit").click()
except:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha-"))).click()
This method doesn't seem to work when Recaptcha doesn't pop up I'm guessing because the next portion of code is attempting to solve the Recaptcha and since it doesn't pop up it causes an error.
The 2nd method I tried
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[name^='a-'][src^='https://www.google.com/recaptcha/api2/anchor?']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//span[#id='recaptcha-anchor']"))).click()
driver.switch_to.default_content()
try:
if(len(driver.find_elements(By.XPATH, "btnSSSubmit"))) > 0 :
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='recaptcha challenge expires in two minutes']")))
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#recaptcha"))).click()
except:
pass
This method doesn't work if the Recaptcha doesn't appear.
Any help please would be appreciated.
Try like below once.
Since its the Recaptcha that may or may not appear, attempt to click on the Recaptcha in the Try block. And then click on the Submit button.
search_box = driver.find_element(By.ID,"SearchCriteria")
search_box.send_keys("Test")
try:
# Try to solve the Recaptcha.
except:
print("Recaptcha did not appear")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID,"btnSSSubmit"))).click()

How to interact with a non-select dropdown with selenium python

I am currently learning the use of selenium with python, and tried to collect some data. I have been struggling for the last couple days with clicking on a dropdown not accessible by Select method.
I looked at A LOT of questions on SOF, blogs, tutorials ... and could not find the answer to my problem.
The dropdown is accessible to this website <"https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/">, then by clicking on the "Box Score" tab. Just below the teams flags, you'll see the dropdown with "ALL SETS" writing in it.
I would like to access the data from "SET 1", "SET 2", "SET 3". My guess would be to click on the dropdown, then click on "SET 1" and so on. But I couldn't make the code work to click on the dropdown.
Below is my code :
PATH = "C:\Program Files\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")
#implicit wait to be sure the elements we want are loaded when we try accessing them
driver.implicitly_wait(5)
actions = ActionChains(driver)
#clicking on button
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "tab-title_boxscore")))
element.click() #mimic clicking on the clickable element
dropdown = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.LINK_TEXT, "ALL SETS"))).click()
first_set = WebDriverWait(driver, 10).until(EC.element_to_be_clickable(
(By.LINK_TEXT, "SET 1"))).click()
Many thanks for your time and answer !
To click() on the element with text as ALL SETS and then to click on the element with text as SET 1 instead of presence_of_element_located() you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following Locator Strategies:
Code Block:
driver.get("https://en.volleyballworld.com/volleyball/competitions/olympics-2020/schedule/11349/")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_boxscore"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.tab-title_all"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='All Sets']//following::li[1]/a/span"))).click()
Browser Snapshot:

Clicked button doesn't really function by Selenium Python

I've been trying to click a button that downloads a CSV file from "https://mol.org/regions/?regiontype=countries". I'm sure that I've selected the button, as I can print the text written on it, but whenever I try to .click() it, it doesn't download the file. Are there any additional steps needed to operate the function bound to the button? Thank you in advance.
PS : The button works manually.
Here is the driver code I used :
with webdriver as driver:
driver.maximize_window()
driver.implicitly_wait(30)
driver.get(url)
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, search_bar_CSS_Selector))).send_keys(search_query+Keys.RETURN)
driver.find_element_by_css_selector(download_button_CSS_Selector).click()
print(driver.find_element_by_css_selector(download_button_CSS_Selector).text)
driver.close()
You can see that I actually print the button text & can access it, but the .click() is not working as expected.
Variables :
search_query = 'Egypt'
search_bar_CSS_Selector = "input[placeholder='Filter Political boundaries']"
download_button_CSS_Selector = "button[ng-click ='initiateDownload()']"
Your css selector looks perfect, but I think it's a page loading issue. So I tried that with an explicit wait command (check below) and it seems working fine.
Sample code :
so instead of this :
driver.find_element_by_css_selector(download_button_CSS_Selector).click()
use this :
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()
Update 1 :
driver.get("https://mol.org/regions/?regiontype=countries")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[placeholder='Filter Political boundaries']"))).send_keys('Egypt'+Keys.RETURN)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[ng-click ='initiateDownload()']"))).click()

How to click the Continue button within https://in.indeed.com/ website using selenium in python?

I am trying to write a code that is able to auto apply on job openings on indeed.com. I have managed to reach the last stage, however, the final click on the application form is giving me a lot of trouble. Please refer the page as below
Once logged in to my profile, I go to the relevant search page, click on the listing I am interested in and then on the final page (shown above) I am trying to click on the continue button using xpath. For the previous step (also a page on the same form), the form had multiple iframes which I was able to switch successfully. Now for the current page, I am stuck as the click function does not do anything. I have written the following code:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
driver.find_element_by_xpath('//*[#id="apply-button-container"]/div[1]/span[1]').click()
time.sleep(5)
frame_1 = driver.find_element_by_css_selector('iframe[title="Job application form container"')
driver.switch_to.frame(frame_1)
frame_2 = driver.find_element_by_css_selector('iframe[title="Job application form"]')
driver.switch_to.frame(frame_2)
continue_btn = driver.find_element_by_css_selector('#form-action-continue')
continue_btn.click()
driver.find_element_by_xpath('//*[#id="form-action-continue"]').click()
I have tried switching the iframes again for this step but nothing happens. Even the .click() function does not do anything.
Will appreciate some help on this.
The element Continue is within nested <iframe> elements so you have to:
Induce WebDriverWait for the parent frame to be available and switch to it.
Induce WebDriverWait for the child frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable.
You can use either of the following Locator Strategies:
Using CSS_SELECTOR:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.indeed-apply-button-label"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='indeedapply-modal-preload']")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#form-action-continue"))).click()
Using XPATH:
driver.get("https://in.indeed.com/jobs?q=data%20analyst&l=Delhi&vjk=5c0bd416675cf4e5")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='indeed-apply-button-label']"))).click()
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(#id, 'indeedapply-modal-preload')]")))
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#title='Job application form']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='form-action-continue']"))).click()
Note : You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
Reference
You can find a couple of relevant discussions in:
Ways to deal with #document under iframe
Switch to an iframe through Selenium and python
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element while trying to click Next button with selenium
selenium in python : NoSuchElementException: Message: no such element: Unable to locate element

Python Selenium webdriver timeout exception

I'm working on a project to extract some data from a website. In this website there is search form that I should fill it. One of the inputs which is text, shows a suggestion after entering 2 or 3 characters and I should select that option in order to go forward or search button will be activated. The problem is that when I use the following code:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='LocationSuggestionBox']/ul/div/li/div"))).click()
I modified the xpath in above code. The actual xpath is as fllow:
//*[#id="LocationSuggestionBox""]/ul/div/li/div
But I don't know how to add it in my code to not get the syntax error.
The final result with my working code is :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='LocationSuggestionBox']/ul/div/li/div"))).click()
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/selenium/webdriver/support/wait.py", line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Your XPath is returning NULL when I run against the page, so the selector is incorrect here.
Based on the page info you provided, here's a correct selector:
"//li[div/span[text()='" + locationNameHere + "']]"
So you can change your code to:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[div/span[text()='" + locationNameHere + "']]"))).click()
If you just want to click the first location suggestion, you can use this:
//li[div/span]
But this XPath will get you a list of ALL visible location suggestions.
Induce WebDriverWait and element_to_be_clickable() And following xpath.
driver.get('https://locatr.cloudapps.cisco.com/WWChannels/LOCATR/openBasicSearch.do;jsessionid=8CDF9284D014CFF911CB8E6F81812619')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='searchLocationInput']"))).send_keys('China')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[#class='ng-scope']//span[text()='CHINA']"))).click()
Browser snapshot:

Categories

Resources