How to press a button with selenium and xpath - python

I need to press a button from a webpage with selenium,but that button has no id.
Other buttons from same webpage are having an id so no problem with pressing that ones with following code:
driver.find_element_by_id('some_id').click()
When i use firebug and firepath on that problem button output is:
<span class="linkRosso"> Invia truppe</span>
xpath:
html/body/div[6]/div[1]/div/div[3]/div/div[2]/table/tbody/tr/td[1]/a[3]/span
Is this information useful??

Use find_element_by_xpath.
xpath = 'html/body/div[6]/div[1]/div/div[3]/div/div[2]/table/tbody/tr/td[1]/a[3]/span'
driver.find_element_by_xpath(xpath).click()

Related

SELENIUM PYTHON ElementClickInterceptedException:

I'm writing a Selenium Python script. Unfortunately, I can't click "Sign in" button or check mark.
I've tried many ways. I'll be glad if anyone helps me plz.
Link:
https://stagingskateontario.memberlounge.ca/auth/login
My Code:
wait.until(EC.element_to_be_clickable((By.ID, ":r2:")))
driver.find_element(By.ID,":r2:").click()
Error:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element is not clickable at point (790, 723)
(Session info: chrome=103.0.5060.134)
enter image description here
I was able to click the button with javascript
element = browser.find_element_by_xpath("/html/body/div/main/div/div/div[2]/form/button")
browser.execute_script("arguments[0].click();", element)
JavaScriptExecutor is an Interface that helps to execute JavaScript through Selenium Webdriver. JavaScriptExecutor provides two methods “executescript” & “executeAsyncScript” to run javascript on the selected window or current page.

"Element Not Interactable" Error using Selenium on a clickable object

I'm new to using Selenium with Python. I'm trying to click on this sign-in button on glassdoor: Glassdoor Sign-in Button
Actual: Using both the XPATH and the classname "LockedHomeHeaderStyles__signInButton", I keep getting the "ElementNotInteractableException" thrown.
Expected: I should be able to select the element and click it.
When I open the site and try to manually click the button myself, I am able to do this. Am I doing something wrong in my code?
def sign_in():
sign_in_element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, "LockedHomeHeaderStyles__signInButton"))
)
sign_in_element.click();
There are two button element with "Sign In" text. You need second one. Try using the below xpath-
//button[text()="Sign In" and not(contains(#class,"d-flex"))]

How would I locate a button element which are in the same list and have the same class but have different text on the button in selenium?

This is the html code from the website:
Screenshot from inspect element
Basically what I would like to do is to find the button but all of the buttons have the same class.
you can simply use the xpath and get the web element
WebElement button = driver.findElement(By.xpath("//button[text()='Buy $100']"));
According to Selenium documentation
You can locate it by xpath:
from selenium.webdriver.common.by import By
mytext = "special button"
driver.find_element(By.XPATH, '//button[text()="{}"]'.format(mytext))
To find a button that says "special button"

How can I click on this check box using Selenium Python?

I can click all of the other checkboxes on the page. But when it comes to this one, it won't allow me to click on it
The HTML code for the checkbox is:
<input id="ContentPlaceHolder1_wucSignInStep2_chkTC" type="checkbox" name="ctl00$ContentPlaceHolder1$wucSignInStep2$chkTC">
My code for clicking the text box:
element = driver.find_element_by_xpath('//span[span/input[#name="checkbox checkbox-primary"]]').click()
I can provide the full code if required.
There is an id associated with your input field! You can use the id to find the element
element = driver.find_element_by_id('ContentPlaceHolder1_wucSignInStep2_chkTC').click()
That should do it.
If you are getting an element not visible error then you can try the following:
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("ContentPlaceHolder1_wucSignInStep2_chkTC")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
driver.execute_script("arguments[0].click();", element)
The above code will make the element visible and also, put the mouse cursor over the checkbox.

Get link in popup window (python, selenium)

I want to get a link in a popup window of this page.
http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx
Clicking the first link that appears the page show us a popup window. I like to get the link inside of thin window.
Here is my code. I appreciate any help.
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://stivconsultasexternas.cnbv.gob.mx/ConsultaInformacionEmisoras.aspx")
link=driver.find_elements_by_partial_link_text(u"Verum")
time.sleep(5)
link[0].click()
time.sleep(5)
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window.
Thank you so much.
The problem you're running into is that the popup is in an iframe. Selenium can only access the current content, anything in a child iframe is inaccessible. In order to allow Selenium to access the new iframe, you'll need to use switch_to.frame:
link[0].click()
time.sleep(5)
driver.switch_to.frame("DefaultPlaceholder_callbackPanel_Popup_CIF-1")
print driver.page_source.encode("utf_8","ignore")#Here I want a html of a popup window.
driver.switch_to.default_content()
driver.find_element_by_xpath("//td[#id='DefaultPlaceholder_callbackPanel_Popup_HCB-1']").click()
Edit #1:
I looked at the page, and found the popin you wanted to access was contained in this iframe:
<iframe id="DefaultPlaceholder_callbackPanel_Popup_CIF-1" title="" src="/Detalle.aspx?enc=vxwQ2MoMebbVt2C93KNYJA%3d%3d" scrolling="auto" frameborder="0" style="height: 578px; width: 100%;">
switch_to.frame() can accept the following as an identifier:
- The name or id of the iframe. I used this particular case, since the iframe in question has id="DefaultPlaceholder_callbackPanel_Popup_CIF-1"
- The index of the iframe. That would looks something like switch_to.frame(0)
- The iframe element. You could find the iframe element via a find_element_by_ locator, then use the webelement result of that in switch_to.frame(el)
Edit #2:
To close the dialog, you need to use switch_to.default_content(), as the close button is outside of the iframe. You can then click the close button of the dialog, and proceed. See the last line in the code above, that line should close the dialog for you.

Categories

Resources