Selenium python - can't find the right path to the button - python

I can't seem to find the right path for the button marked above. Any help on how to simulate a click on this button?
This is the website, and linked under is a greater part of the source code.

To click on the element with text as Lukk 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, "ytcp-button#close-button div[label='Lukk']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ytcp-button[#id='close-button']//div[#label='Lukk' and text()='Lukk']"))).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

I would say to first wait for the popup to open. Explicit wait can do:
Here is a sample code:
https://seleniumbyexamples.github.io/waitvisibility
Your selector would be paper-dialog#dialog
Once the popup is visible you can now wait for the button.
You can use wait to be clickable:
https://seleniumbyexamples.github.io/waitclickable
The id would be close-button
You can also try other locators:
https://seleniumbyexamples.github.io/locator

Related

selenium python how to close popu tab

if I click any of menu on this page it will show menu info. But how I can close the popup ? see the screenshot
I tied those code but didn't work.
driver.find_element_by_css_selector('.styles__ActionWrapper-sc-v9lptc-1 svg') , driver.find_element_by_xpath("//div[#class='styles__ActionWrapper-sc-v9lptc-1 eekxlt']")
As I see
driver.find_element_by_css_selector('.styles__ActionWrapper-sc-v9lptc-1 button')
Or
driver.find_element_by_xpath("//div[#class='styles__ActionWrapper-sc-v9lptc-1 eekxlt']/button")
Should work, but you have to add a delay to make these elements fully loaded before clicking them.
The best way is to use visibility_of_element_located expected condition explicit wait.
Like this:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 20)
wait.until(EC.visibility_of_element_located((By.CSS_SELECTOR, ".styles__ActionWrapper-sc-v9lptc-1 button"))).click()
The desired element is a dynamic element, so to click on the clickable 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, "div[class^='styles__ActionWrapper'] button[class^='styles__StyledButtonRoot']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(#class, 'styles__ActionWrapper')]//button[starts-with(#class, 'styles__StyledButtonRoot')]"))).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
I'm sure the other two answers are fine (I didn't test them, but I suppose their authors did) but here is my take on the whole issue, difference being the way I select the item as seen from a US connection:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#aria-label = 'Close Combo #1']"))).click()
This assumes you selected Combo #1 from the menu. You can change that locator dynamically, depending on the menu selected.
ChromeOptions options = new ChromeOptions()
options.setExperimentalOption("excludeSwitches",
Arrays.asList("disable-popup-blocking"))
Try this code

Finding a button with selenium in python

I am trying to create a web application that automately republishes on a site for private sales.
Login and everything went well, but if I try to find and click the republish button it just wont work.
I've tried it with every locator, but the problem is that every button got an uniqe ID.
Example:
<button name="republish" type="button" data-testid="5484xxxxx-republish-button" class="Button__ButtonContainer-sc-3uxxxx-0 hxXxxX">Republish</button>
The last I've tried:
buttons = driver.find_elements(By.XPATH, "//*[contains(text(), 'Republish')]")
for btn in buttons:
btn.click()
But it also didnt work, same with By.NAME, BY.TAG_NAME
The <button> element looks to be dynamically generated so to click elements you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-testid$='republish-button'][name='republish']"))))
Using XPATH:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[starts-with(#class, 'Button__ButtonContainer') and contains(#data-testid, 'republish-button')][#name='republish' and text()='Republish']"))))
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

Automate web driver to visit the next page page using Selenium

I am trying to automate going to the next product list in the following website.
https://munnalaldawasaz.in/categories/essential-oils
I don't see any change in the url while visiting the next page.
I have attached an image for element that is to be clicked.
Image:
To click on the element > you can use either of the following Locator Strategies:
Using xpath:
driver.find_element(By.XPATH, "//article/div[#class='toolbar']//div[#class='pages']//ul[#class='pagination']//a[text()='Last']//preceding::li[1]/a").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 XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//article/div[#class='toolbar']//div[#class='pages']//ul[#class='pagination']//a[text()='Last']//preceding::li[1]/a"))).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

Can't seem to select from drop down menu - Python Selenium

As titled, seems like I just can't select the drop down menu from this website no matter what.
from selenium import webdriver
from selenium.webdriver.support.ui import Select
driver=webdriver.Chrome()
driver.get('https://assessing.nashuanh.gov/search.asp')
time.sleep(1)
select=Select(driver.find_element_by_xpath('//*[#id="cboSearchType"]'))
select.select_by_value('2')
First you have to handdle the frames in your page. Also looks like there is no value 2 inside this dropdown, so you have to pass a valid value.
driver.switch_to.frame("middle")
select = Select(driver.find_element_by_xpath('//*[#id="cboSearchType"]'))
select.select_by_value('Parcel')
The <option> element with value/text as Owner within the html-select is within an <frame> so you have to:
Induce WebDriverWait for the desired 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 and select_by_value():
driver.get('https://assessing.nashuanh.gov/search.asp')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='middle']")))
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "select#cboSearchType")))).select_by_value("Owner")
Using XPATH and select_by_visible_text():
driver.get('https://assessing.nashuanh.gov/search.asp')
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[#name='middle']")))
Select(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//select[#id='cboSearchType']")))).select_by_visible_text("Owner")
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

How to click on the download button using Selenium and Python

I am trying to download an excel file using Selenium Python using the below code:
download=driver.find_element_by_css_selector("span[data-reactid='.0.1.0.0.1.1.1.0.1.0.1'>Download Orders]")
The element of the download button look like:
The piece of code above isnt working. Can someone help?
Your CSS locator is wrong. Please try to click with below xpath.
//span[.='Download Orders']
The desired element is a React element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
Using XPATH with normalize-space():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[normalize-space()='Download Orders']"))).click()
Using XPATHand contains():
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[contains(., 'Download Orders')]"))).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
I agree with Muzzamil that the CSS locator you have used seems wrong. # is needed to select the attribute data-reactid.
You can try XPath:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1']
If you want to include also the text, try:
//span[#data-reactid='.0.1.0.0.1.1.1.0.1.0.1'][contains(text(),'Download Orders')]

Categories

Resources