How to find selector for this HTML snippet - python

How to find selector from this? I want to click the button but unable to find selector.it will be a css selector or class selector.
HTML
<button data-bb-handler="confirm" type="button" class="btn btn-success">Continue and Logout Other User</button>

You can try it
button_Login = driver.find_element_by_css_selector("#button").text("Continue and Logout Other User")

Make sure the button is visible when you try to click it.
The following selectors should work:
.btn-succes
//button[#class='btn btn-succes']

You can use the following selectors -
driver.find_element(By.CSS_SELECTOR,'button.btn.btn-success')
driver.find_element(By.CSS_SELECTOR, 'button[class='btn btn-success']')
driver.find_element(By.XPATH, "//button[normalize-space()='Continue and Logout Other User']")

Related

Can't Click Radio Button in Selenium Using Python

Here is the HTML Code:
<div class="ui-helper-hidden-accessible">
<input type="radio" name="group2" value="yes">
</div>
Full script
Here are the code which i've tried to click the radio button:
driver.find_element(By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]').click()
AND
rdbutton = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[name= "group2"][value="yes"]'))).click()
AND
rdbutton = driver.find_elements(By.XPATH, "//input[#name='group2']")
for rdbuttons in rdbutton:
if rdbuttons.is_selected():
pass
else:
rdbuttons.click()
It seems that you are using wrong locator. From the HTML you have given i think you are trying to click on radio button that have label Ya if this is the case then try with below xpath:
"//label[contains(text(),'Ya')]"
OR
"//*[normalize-space()='Ya']"
If this does not solve your problem, so please explain and add HTML of proper element either by screenshot or in textual format

how to click button in python selenium?

<div class="gift">
<button type="button" data-log-actionid-label="gift" data-log-body="{"current_product_no" : "1003072802", "group_product_no" : "", "group_product_firstview_no" : "", "product_price" : "5600", "send_impression": "Y"}" data-is-send-log="true">
togift
<span class="ic_new">new</span>
</button>
</div>
Can't use by text, because this page have lots of text togift
How can I click this button ?
You can try using the class name:
div = driver.find_element_by_class_name('gift')
btn = div.find_element_by_xpath('.//button')
btn.click()
If there are other tags with the same class name, then you can use the css selector or the full xpath
Should click on the div class gift with button.
driver.find_element_by_xpath("div[class='gift']/button").click()
Not enough html source to let us know how to write a correct and short element's location statement.
So just try the below xpath expressions:
"//div[#class='gift']/button[contains(text(),'togift') and ./span[contains(text(),'new')]]"
"//div[#class='gift']/button[contains(text(),'togift')]"
"//div[#class='gift']/button"

Element not being clicked even though it is found using Selenium

I'm trying to click on an element (radio button) using Selenium (in Python), I can't disclose the URL because it's a private corporate intranet, but will share the relevants part of code.
So basically this element is within an iframe, thus, I've used the following code to get the element:
# Select the item on main DOM that will udpate the iframe contents
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//*[#id='sm20']"))).click()
# Don't sleep, but only WedDriverWait...
wait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[#name='ifrInterior']")))
# Select the element inside the iframe and click
wait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo']"))).click()
The HTML code for the element is this:
<span class="W1">
<input type="radio" name="gestionesPropias_Equipo" value="1" onclick="javascript:indicadorPropiasEquipo(); ocultarPaginacion('1'); limpiarDatosCapaResultado();">
</span>
I'm interested in clicking this because when we click on it a drop-down is enabled:
If clicked then the dropdown is enabled:
The intersting HTML code for this is
<span id="filtroAsignado" class="W30">
<select name="nuumaAsignado" class="W84">
<option value="">[Todo mi equipo]</option></select>
</span>
Debugging a bit Selenium I can see that the elemtn is found:
And this is actually the base64 image of the element, which is the expected radio button
So I'm wondering why the element actually does not get clicked??
UPDATE: Based on request from #DebanjanB, I'm adding the HTML code from the iframe, which is enclosed inside a div in the main page:
<div id="contenido">
<iframe frameborder="0" id="ifrInterior" name="ifrInterior" src="Seguimiento.do?metodo=inicio" scrolling="auto" frameborder="0"></iframe>
</div>
Actually if I look for the word "iframe", there's only one...
Now checking the iframe source itself, has several iframes hidden but the element I need to interact with is in the iframe mentioned above, the only thing that I forgot to mention is that it's inside a form, but I guess that's not relevant? You can see the whole structure in the following image:
Great question. If you know that selenium found the element, you can use Javascript to click the element directly.
The syntax is:
driver.execute_script("arguments[0].click();", element)
You can also do this, which works the same way:
automate.execute_script("arguments[0].click();", wait.until(EC.element_to_be_clickable((By.XPATH, 'Your xpath here'))))
Essentially you are having Selenium run a javascript click on the element you have found which bypasses Selenium. Let me know if this helps!
I don't see any such issue with your code block either. Perhaps you can try out either of the following options:
Using ActionChains:
ActionChains(driver).move_to_element(WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']")))).click().perform()
Using executeScript() method:
driver.execute_script("arguments[0].click();", WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#name='gestionesPropias_Equipo' and #type='radio']"))))

I cannot create the .click() action on the button

I cannot create the .click()-action on the button.
HTML:
<button class="btn__type btn__type_close">Закрыть</button>
I try to make an element clickable for execute_script:
driver.execute_script("return document.getElementsByClassName('.btn__type_close')").click()
My code using Selenium:
driver.find_element_by_css_selector('button.btn__type_close').click()
None of the methods is working.
Question: How will it be correct?
Did you try clicking using XPath?
WebElement btnClose=driver.findElement(By.xpath("//button[#class='btn__type btn__type_close']"));
if(btnClose.isEnabled()){
btnClose.click();
}

I m not able to click on a button in a web page using selenium in python

What is wrong in the below code
import os
import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://x.x.x.x/html/load.jsp")
elm1 = driver.find_element_by_link_text("load")
time.sleep(10)
elm1.click()
time.sleep(30)
driver.close()
The page source is
<body>
<div class="formcenterdiv">
<form class="form" action="../load" method="post">
<header class="formheader">Loader</header>
<div align="center"><button class="formbutton">load</button></div>
</form>
</div>
</body></html>
I want to click on button load. when I ran the above code getting this error
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: load
As the documentation says, find_elements_by_link_text only works on a tags:
Use this when you know link text used within an anchor tag. With this
strategy, the first element with the link text value matching the
location will be returned. If no element has a matching link text
attribute, a NoSuchElementException will be raised.
The solution is to use a different selector like find_element_by_class_name:
elm1 = driver.find_element_by_class_name('formbutton')
Did you try using Xpath?
As the OP said, find_elements_by_link_text works on a tags only:
Below code might help you out
driver.get_element_by_xpath("/html/body/div/form/div/button")

Categories

Resources