Click a button using Selenium on python - python

I am trying to click this button to export my data as a csv file. I'm using Selenium on python.
I can not figure out how to press the button because there is no id or name and the link changes every time.
Thanks in advance!

You have to scroll to that element. Like this:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_xpath("//a[contains(#href,'swissadme.csv')]")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
element.click()
If the locator above not enough try this instead:
"//a[contains(#href,'results/50881847/swissadme.csv')]"

See if this works:-
elm = driver.find_element_by_xpath(".//div[contains(text(),'Retrieve date:')]/a[contains(#href,''swissadme.csv)]")
driver.execute_script("arguments[0].scrollIntoView(true);",elm);
elm.click()

Related

How can I use the inside of the #document on a website, with selenium, to click on buttons?

I have been trying to automate a game I found by clicking buttons with selenium, however in doing so I found troubles in getting past the iframe and #document.
Here is the game I'm trying to automate: https://www.abcya.com/games/jet_ski_addition
I want to access a button on the website to start the game, but I cannot access this button. I have tried switching frames, I don't know if that worked but I think there are nested html files?
My ultimate goal is to be able to click on the buttons automatically, thanks for any help!
Here is the current code:
# Store iframe web element
iframe = driver.find_element(By.CSS_SELECTOR, "#desktopGame > iframe")
# switch to selected iframe
driver.switch_to.frame(iframe)
# Now click on button
#driver.find_element(By.TAG_NAME, 'PlayChevrons').click()
driver.find_element(By.ID, "main").click()
d = driver.find_element(By.TAG_NAME, "use")
button = driver.find_element_by_xpath("//use[#href='#PlayChevrons']")
button.click()
button = driver.find_element_by_xpath("//use[#href='#PlayButton']")
button.click()
The Play button is inside svg > g tags, you have to handle the elements inside svg or g tags like below:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 15)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "#desktopGame > iframe")))
time.sleep(1)
driver.find_element(By.XPATH, "//*[name()='svg']//*[local-name()='g' and #id='main']//*[local-name()='g' and #cursor='pointer']").click()
You can refer the below links to know how to handle svg and g tags:
https://medium.com/illumination/automating-svg-elements-with-selenium-a7c31f99ebf8
https://www.linkedin.com/pulse/designing-xpath-svg-elements-suresh-dubey/?trk=public_profile_article_view
https://www.tutorialspoint.com/clicking-on-elements-within-an-svg-using-xpath-selenium-webdriver#:~:text=New%20Selenium%20IDE&text=To%20click%20the%20element%20with,the%20build%20and%20execute%20methods.

How can I fill the form of this popup?

I'm just learning Selenium and I wrote this small code. From there, I don't find any way to fill a form. The HTML source of the fill popups appears only when you clic "Créer un compte", "Create an account" in english.
But it looks like when I just print(driver.source_page) I don't get this part of the code.
from selenium import webdriver
from selenium.webdriver.common.by import By
import webbrowser
driver = webdriver.Chrome()
driver.get('http://labrute.muxxu.com/')
element = driver.find_element(By.CLASS_NAME,'tid_long')
element.click()
driver.switch_to_frame(driver.find_element_by_css_selector('//*[#id="create"]/div[1]/div[1]/div[1]/div/label'))
Do you have any ideas?
Here is the popup
Here is without popup
Thanks
I tried to use like driver.switch_to xx methods, but nothing worked yet.
It is inside an iframe, you have to switch to the iframe then you have to enter the values:
WebDriverWait(driver, 15).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "//*[#name='tid_create']")))
time.sleep(1)
driver.find_element(By.XPATH, "//*[#name='name']").send_keys("name")
Thanks for your help,
The code finally looks like:
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR,'#tid_frameWrapper
> iframe'))
Best regards

How to make nonclickable button click able in python using selenium?

Hi Before starting Thanks for the help in advance
So I am trying to scrape google flight website : https://www.google.com/travel/flights
When scraping I have done the sending Key to the text field but I am stuck at clicking the search button it always gives the error that the field is not clickable at a point or Other elements would receive the click
the error image is
and the code is
from time import sleep
from selenium import webdriver
chromedriver_path = 'E:/chromedriver.exe'
def search(urrl):
driver = webdriver.Chrome(executable_path=chromedriver_path)
driver.get(urrl)
asd= "//div[#aria-label='Enter your destination']//div//input[#aria-label='Where else?']"
driver.find_element_by_xpath("/html/body/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[1]/div[2]/div[1]/div[4]/div/div/div[1]/div/div/input").click()
sleep(2)
TextBox = driver.find_element_by_xpath(asd)
sleep(2)
TextBox.click()
sleep(2)
print(str(TextBox))
TextBox.send_keys('Karachi')
sleep(2)
search_button = driver.find_element_by_xpath('//*[#id="yDmH0d"]/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[2]/div/button/div[1]')
sleep(2)
search_button.click()
print(str(search_button))
sleep(15)
print("DONE")
driver.close()
def main():
url = "https://www.google.com/travel/flights"
print(" Getitng Data ")
search(url)
if __name__ == "__main__":
main()
and i have done it by copying the Xpath using dev tools
Thanks again
The problem you are facing is that after entering the city Karachi in the text box, there is a suggestion dropdown that is displayed over the Search Button. That is the cause of the exception as the dropdown would receive the click instead of the Search button. The intended usage of the website is to select the city from the dropdown and then continue.
A quick fix would be to first look for all of the dropdowns in the source (there are a few) and look for the one that is currently active using is_displayed(). Next you would select the first element in the dropdown suggested:
.....
TextBox.send_keys('Karachi')
sleep(2)
# the attribute(role) in the dropdowns element are named 'listbox'. Warning: This could change in the future
all_dropdowns = driver.find_elements_by_css_selector('ul[role="listbox"]')
# the active dropdown
active_dropdown = [i for i in all_dropdowns if i.is_displayed()][0]
# click the first suggestion in the dropdown (Note: this is very specific to your search. It could not be desirable in other circumstances and the logic can be modified accordingly)
active_dropdown.find_element_by_tag_name('li').click()
# I recommend using the advise #cruisepandey has offered above regarding usage of relative path instead of absolute xpath
search_button = driver.find_element_by_xpath("//button[contains(.,'Search')]")
sleep(2)
search_button.click()
.....
Also suggest to head the advise provided by #cruisepandey including research more about Explicit Waits in selenium to write better performing selenium programs. All the best
Instead of this absolute xapth
//*[#id="yDmH0d"]/c-wiz[2]/div/div[2]/c-wiz/div/c-wiz/c-wiz/div[2]/div[1]/div[1]/div[2]/div/button/div[1]
I would recommend you to have a relative path:
//button[contains(.,'Search')]
Also, I would recommend you to have explicit wait when you are trying a click operation.
Code:
search_button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Search')]")))
search_button.click()
You'd need below imports as well:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Pro tip:
Launch the browser in full screen mode.
driver.maximize_window()
You should have the above line just before driver.get(urrl) command.

Cannot click on an xpath selected object Selenium (Python)

I am trying to click to an object that I select with Xpath, but there seems to be problem that I could not located the element. I am trying to click accept on the page's "Terms of Use" button. The code I have written is as
driver.get(link)
accept_button = driver.find_element_by_xpath('//*[#id="terms-ok"]')
accept_button.click()
prov = driver.find_element_by_id("province-region")
prov.click()
Here is the HTML code I have:
And I am getting a "NoSuchElementException". My goal is to click this "Kabul Ediyorum" button at the bottom of the HTML code. I started to think that we have some restrictions on the tasks we could do on that page. Any ideas ?
Not really sure what the issue might be.
But you could try the following:
Try to locate the element by its visible text
accept_button = driver.find_element_by_xpath("//*[text()='Kabul Ediyorum']").click()
Try with ActionChains
For that you need to import ActionChains
from selenium.webdriver.common.action_chains import ActionChains
accept_button = driver.find_element_by_xpath("//*[text()='Kabul Ediyorum']")
actions = ActionChains(driver)
actions.click(on_element=accept_button).perform()
Also make sure you have implicitly wait
# implicitly wait
driver.implicitly_wait(10)
Or explicit wait
element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//*[text()='Kabul Ediyorum']"))).click()
Hope this helped!

How do I properly click an element in Selenium with Python?

I am trying to click an element, for example "AH", in this page. I use the following code.
from selenium import webdriver
url = "http://www.oddsportal.com/soccer/brazil/serie-a/internacional-santos-vcGTTAKH/"
driver = webdriver.Firefox()
driver.get(url)
element_to_click = driver.find_element_by_link_text("AH")
element_to_click.click()
The problem is that after the element is clicked and the new page is loaded, it goes back to the first page.
Focus the element and call click_and_hold action (worked for me):
from selenium.webdriver import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element_to_click).click_and_hold(element_to_click).perform()
alecxe , that works.
Just to add to the discussion here
So on mouse down it is invoking onClick for the uid(4), when we do a normal click on the element we do not realize that it worked on mouse down not on mouse click.
Thats why when we are using webdriver to do element.click() on it, this does not work and when we use Actions class to simulate mouse down using click_and_hold, It works !!

Categories