#This issue was resolved becouse it was an iframe issue, that's why the element couldn't be found, the code I used to make it work is:
button = driver.find_elements_by_css_selector(".btn.btn-primary")
I have the following html code:
<td>
<input type="hidden" value="xxxxx" id="1_nombre_77074118_1">
<input type="hidden" value="xxx#xxx.cl" id="1_email_77074118_1">
<a class="btn btn-primary" href="" onclick="javascript:submitEntrar(true,1,77074118,'1');return false;">Ingresar como usuario</a>
</td>
I need to click the class="btn btn-primary / Ingresar como usuario" button, but can't seem to find the element?
This is my code:
button = driver.find_elements_by_class_name("btn btn-primary")
print(len(button))
for b in button:
print(button)
(Both print's don't return element's becouse they where not found).
I also tried by XPATH but it didin't work, this is the XPATH:
//*[#id="container"]/section/div[1]/table/tbody/tr[1]/td[3]/a
Full XPATH:
/html/body/div[1]/div[1]/section/div[1]/table/tbody/tr[1]/td[3]/a
Thanks a lot!
First of all since there are multiple class names in the element instead of
button = driver.find_elements_by_class_name("btn btn-primary")
Try
button = driver.find_elements_by_css_selector(".btn.btn-primary")
Also you possibly have to add delay / wait before accessing these elements.
Try
button = driver.find_elements_by_css_selector(".btn.btn-primary)
can you try this as we can search and click on the element based on the text
driver.find_element_by_xpath("//a[text()='Ingresar como usuario']")
driver.find_element_by_xpath("//*[contains(text()='Ingresar como usuario')]")
driver.find_element_by_partial_link_text("Ingresar como usuario")
Also, recommend using explicitWait
button = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.XPATH,"//a[text()='Ingresar como usuario']")))
button.click()
It is because of two reasons
Class call should prepend with . Eg) .btn
You are trying to call with two classes. If you notice btn and btn-primary are two different classes, so you could call using either
button = driver.find_elements_by_css_selector(".btn.btn-primary")
Or
button = driver.find_elements_by_class_name(".btn.btn-primary")
Related
I want to click this button in a website using selenium but I am not sure how to select it.
<a data-gfm-analytics-element="btn_showmore_browse" data-area="norma_category" class="button hollow expanded-mobile js-load-more-results" href="#" style="display: inline-block;">Show More</a>
I used this code to do it but it returns NoSuchElementException: Message: no such element
btn = driver.find_element(By.CLASS_NAME,'button hollow expanded-mobile js-load-more-results')
button hollow expanded-mobile js-load-more-results are actually 4 class names while By.CLASS_NAME receives single class name value. You can use CSS Selector or Xpath with multiple class names
btn = driver.find_element(By.CSS_SELECTOR,'a.button.hollow.expanded-mobile.js-load-more-results')
Also btn_showmore_browse seems to be unique value, so possibly this will work too:
btn = driver.find_element(By.CSS_SELECTOR,'a[data-gfm-analytics-element="btn_showmore_browse"]')
There are more possible options
There is a radio button whose buttons are controlled by <input/>. Its code is this:
<input id="IsEmployementProvided_0" name="IsEmployementProvided" type="radio" value="1" style="" xpath="1">
<input checked="checked" id="IsEmployementProvided_1" name="IsEmployementProvided" type="radio" value="0" xpath="1" style="">
As can be seen, the difference is the attribute "checked". So I am trying to find which one has a dot inside it, I mean which one has an attribute "checked". Here what I tried:
if(driver.find_element_by_xpath("//input[#id='IsEmployementProvided_1']").get_attribute("checked").contains("checked")):
print("value = "+driver.find_element_by_xpath("//input[#id='IsEmployementProvided_1']").get_attribute("checked").text)
Here error comes: Element has no attribute "contains". I am trying to determine if element has attribute "checked". Any help?
If you try to get the value of checked you get true:
driver.find_element(By.XPATH, "//input[#id='IsEmployementProvided_1']").get_attribute("checked")
#Output
'true'
Whereas, it returns None for the other radio button, IsEmployementProvided_0.
So, you can do something like this:
if driver.find_element(By.XPATH, "//input[#id='IsEmployementProvided_1']").get_attribute("checked") == 'true':
print('This is for checked!')
# Do something
# OR simply skip the "== true" part.
if driver.find_element(By.XPATH, "//input[#id='IsEmployementProvided_1']").get_attribute("checked"):
print('This is for checked!')
# Do something
Also, find_elements_by_xpath is deprecated. Please use find_element instead. You'll have to import By for this:
from selenium.webdriver.common.by import By
I have used the below code to click the element.But it failed to locate the element and shows element not visible.
elem3=driver.find_element_by_xpath(".//*[#id='check-box']")
elem3.click()
The html code:
<span id="Some-span" class="urCWhl" title="Indicator">
<input id="check-box" class="urC" type="checkbox" hidefocus="hidefocus" ti="-1" tabindex="-1" ct="C"/>
<span id="label-lbl" class="name_class" style="width:100%;box-sizing:border-box;" unselectable="on" f="some-id" ti="0" tabindex="0" title="Indicator"></span>
You can try something like this:
element = driver.find_element_by_xpath(".//*[#id='check-box']")
driver.execute_script("arguments[0].click();", element)
The input might be inside a frame? If so switch to that frame by doing:
driver.switch_to_frame('framename')
or if not, try to find via id then click the element:
driver.find_element_by_id('check-box').click()
one thing to remember is that if the checkbox is already have a value, if you click the checkbox, the check will be removed. if you want to have the checkbox to have a true value always, you may do this:
driver.execute_script("document.getElementById('check-box').setAttribute('checked','');")
This will execute a javascript to always have a true value on the checkbox
<li id="add-to-cart" class="">
<input type="button" value="Add to Cart" class="primary" name="add-to-cart">
</li>
I want to print value
Output: Add to Cart
Here my solution:
first get elements inside < li> (Maybe there will be more than one):
elements = browser.find_elements_by_xpath("//li[#id='add-to-cart']//input")
for e in elements:
print(e.get_attribute("name"))
From Selenium docs:
find the button element using one of the find_element_by... methods on a driver (or on a parent WebElement)
read the name of the button or value of an input using .get_attribute(attribute_name)
read the text using property .text
The webpage i'm starting from is https://classschedule.tulane.edu/Search.aspx . The page source information for the button I need clicked is:
<input type="submit" name="ctl00$MainContent$btnSearchAll" value="All Courses" id="btnSearchAll" class="JQButton ui-button ui-widget ui-state-default ui-corner-all" role="button" aria-disabled="false" autocomplete="off" style="height: 22px;">
I have tried different methods to find this button and click on it such as;
element = browser.find_element_by_id("btnSearchAll")
element = browser.find_element_by_xpath("//input[#id ='btnSearchAll']")
element = browser.find_element_by_name("ctl00$MainContent$btnSearchAll")
I think it is finding the button because when I do...
print element
...this is returned:
<selenium.webdriver.remote.webelement.WebElement object at 0x2b49690>
I have no other ideas on how to make my program click the button.
I think you didn't add the code for clicking on the element. Please do that and check:
element = browser.find_element_by_id("btnSearchAll")
element.click()
I did try the same code as above with JAVA, and it is working fine.
This works well for me
for i in range(1, 10):
try:
driver.find_element_by_xpath(
f'/html/body/div[{i}]/div/div/div/div/div[2]/span[1]/span/span/input').click()
except:
pass