Selenium can't click a button python - python

Can you tell me why selenium can't click a button. I tried xpath, id, class, text and nothing.
i get info that there is no such element or sth like that but in firefox i can see that there's an item the name is the same. No idea whats wrong.
self.driver.execute_script("window.scrollTo(0, 3500)")
sleep(1)
#self.action.move_to_element(przycisk).click(sprawdz).perform()
self.driver.find_element_by_xpath("//button[#id='sprawdz']").click();
#self.driver.find_element_by_link_text("ok").click();

To click on the element you can use either of the following Locator Strategies:
Using css_selector:
self.driver.find_element_by_css_selector("button.btn.btn-large#sprawdz").click()
Using xpath:
self.driver.find_element_by_xpath("//button[#class='btn btn-large' and #id='sprawdz']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-large' and #id='sprawdz']"))).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
Update
As a last resort you can use execute_script() method as follows:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-large#sprawdz"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#class='btn btn-large' and #id='sprawdz']"))))

try this
element = self.driver.find_element_by_xpath("//button[#id='sprawdz']")
self.driver.execute_script("arguments[0].click();", element)

Related

How to use python selenium to click this element with text bb1

<yt-formatted-string id="channel-title" class="style-scope ytd-account-item-renderer">bb1</yt-formatted-string>
below one is a dangerous find element by full x path because the index might change
self.driver.find_element_by_xpath('/html/body/ytd-app/ytd-popup-container/iron-dropdown/div/ytd-multi-page-menu-renderer/div[4]/ytd-multi-page-menu-renderer/div[3]/div[1]/ytd-account-section-list-renderer[1]/div/ytd-account-item-section-renderer/div/ytd-account-item-renderer[4]/paper-icon-item/paper-item-body/yt-formatted-string[1]').click()
To click on the element with text as bb1 you can use either of the following Locator Strategies:
Using css_selector:
self.driver.find_element_by_css_selector("yt-formatted-string.style-scope.ytd-account-item-renderer#channel-title").click()
Using xpath:
self.driver.find_element_by_xpath("//yt-formatted-string[#class='style-scope ytd-account-item-renderer' and #id='channel-title'][text()='bb1']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "yt-formatted-string.style-scope.ytd-account-item-renderer#channel-title"))).click()
Using XPATH:
WebDriverWait(self.driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//yt-formatted-string[#class='style-scope ytd-account-item-renderer' and #id='channel-title'][text()='bb1']"))).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
References
You can find a couple of relevant detailed discussions in:
How to click a link within youtube comment using python selenium

Selenium Python: 'Close' button on pop-up window not interactable

Can't seem to click on a 'close' button to close a pup up window that I have opened before to scrape the data that it displays.
The button has the following html script:
<div class="closebutton" onclick="return hs.close(this)" title="Schließen"></div>
I was trying the following:
driver.find_element_by_xpath("//div[#class='closebutton']").click()
But I get the error:
Message: element not interactable
Anyone have a clue how to make the element interactable?
To click on the element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.closebutton[title='Schließen']").click()
Using xpath:
driver.find_element_by_xpath("//div[#class='closebutton' and #title='Schließen']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.closebutton[title='Schließen']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[#class='closebutton' and #title='Schließen']"))).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

Selenium not able to click on Get Data button on using Python

I am scraping data from this website . The element is below and geckodriver
<img class="getdata-button" style="float:right;" src="/common/images/btn-get-data.gif" id="get" onclick="document.getElementById('submitMe').click()">
but can't get selenium to click it tried even xpath, id but not luck
is there any fix or work around to get it done?
To click on the element Get Data you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("img.getdata-button#get").click()
Using xpath:
driver.find_element_by_xpath("//img[#class='getdata-button' and #id='get']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "img.getdata-button#get"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//img[#class='getdata-button' and #id='get']"))).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
You should probably try by id
driver.find_element(By.ID, 'get').click()

XPATH is changing

I am using Python 3 and Selenium(Chromedriver). I want to check for a element with this command.
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button"))).click()
The problem is that the XPATH is constantly changing between two paths:
/html/body/div[3]/div/div[6]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
/html/body/div[3]/div/div[3]/div[2]/div[2]/div/form/div[2]/div[5]/div/div/div/div[2]/button
I want to tell Python, that if the element is not found, it should search for the other XPATH.
If you know a method to find the element without the XPATH, i would also be happy with the solution.
It does not work if you search for the elemnt by its containing text, because the language from the sites changes if use a proxy.
This is the "Inspect Element" code of the button:
<button aria-label="Mobilnummer hinzufügen" class="bg-white css-1eajgu7 ex41m6f0 btn-secondary-dark " type="button">Hinzufügen</button>
To click on the element with text as Hinzufügen you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']").click()
Using xpath:
driver.find_element_by_xpath("//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']").click()
Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.bg-white.btn-secondary-dark[aria-label='Mobilnummer hinzufügen']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label='Mobilnummer hinzufügen' and text()='Hinzufügen']))).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
Try to use this XPath:
//div[contains(#class, 'mex-mobile-phone')]//button[contains(#class, 'btn-secondary-dark')]

How to click on a javascript/AJAX based element with onclick attribute using Selenium and Python

I'm trying to click a link in a dropdown menu in Selenium.
I'm accessing the element like so:
link = menu.find_element_by_xpath('//*[contains(text(), "Mark as shipped")]')
The link's href is javascript.void(0), and contains an onclick attribute which contains:
'com.ebay.app.myebay2.lineaction.service.LineActionAjax.processTransRequest("http://payments.ebay.com/ws/eBayISAPI.dll?OrderAction&transId=#TID#&action=4&pagetype=1883&ssPageName=STRK:MESO:SHP&itemid=_Item_Id", "_Item_Id", "987349587", "MarkShipped", "98739873", "_Item_Id_9874987_ss", 24")'
I've tried triggering this with:
click()
and
driver.execute_script(link.get_attribute('onclick'))
Also an ActionChain mousing over the link and clicking it.
But none seem to work. How do I trigger this?
The element is a AJAX element, so ideally to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using PARTIAL_LINK_TEXT:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Mark as shipped"))).click()
Using CSS_SELECTOR:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a[onclick*='MarkShipped']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#onclick, 'MarkShipped') and contains(., 'Mark as shipped')]"))).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

Categories