Selenium Click button whitout id - python

i have to click the input type="button" element of the first li, but i can not refer to the id of the li, since it changes every time i i open the website, this is what i tried
driver.find_elements_by_xpath('//input[data-componentname="gender"]').get(0).click()
and it gives me this error:
AttributeError: 'list' object has no attribute 'get'
if i remove the get part, this is the error:
AttributeError: 'list' object has no attribute 'click'
This is the code, like i said, i have to click the first input without referring to the id
<ul data-componentname="gender">
<li id="78ece221-1b64-4124-8483-80168f21805f" class="">
<input type="button">
<span>Uomo</span>
</li>
<li id="2678a655-b610-41e0-8e7f-bad58fbcb1b3" class="">
<input type="button">
<span>Donna</span>
</li>
</ul>

To click on the element Uomo 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, "ul[data-componentname='gender'] li:nth-of-type(1) span"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[#data-componentname='gender']//span[text()='Uomo']"))).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
Browser Snapshot:

find_elements method return a list of web elements.
The first member of the list can be received by simple indexation i.e. list_object[0]
Also, since you want to get the first element matching the //input[data-componentname="gender"] locator you can simply use find_element method.
So, instead of
driver.find_elements_by_xpath('//input[data-componentname="gender"]').get(0).click()
You can use
driver.find_element_by_xpath('//input[data-componentname="gender"]').click()
Or
driver.find_elements_by_xpath('//input[data-componentname="gender"]')[0].click()
UPD
well, you tried to use a wrong locator.
This should work:
driver.find_element_by_xpath("//ul[#data-componentname='gender' ]//span[text()='Uomo']/../input").click()
You can also click the element containing the "Uomo" text itself as well
driver.find_element_by_xpath("//ul[#data-componentname='gender' ]//span[text()='Uomo']").click()

You can include the first li if you only want that. For example:
driver.find_element_by_xpath('//input[data-componentname="gender"]/li[1]')
otherwise if you want the list then you can do
driver.find_elements_by_css_selector('input[data-componentname="gender"] > li')
and do the .get() according to which button you want to click

Related

How to find this element in selenium python

In the web page, there is many elements like this below and I want to find all of them and click one after one.
they have the same name of class but different by ID.
How I can find it and click?
<a class="do do-task btn btn-sm btn-primary btn-block" href="javascript:;" data-task-id="1687466" data-service-type="3" data-do-class="do-task" data-getcomment-href="/tasks/getcomment/" data-check-count="0" data-max-check-count="2" data-money-text="0.08"><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0.08 </font></font><i class="far fa-ruble-sign"></i></a>
To identify all of the similar elements and create a list you have to induce WebDriverWait for visibility_of_all_elements_located() and you can use either of the following Locator Strategies:
Using CSS_SELECTOR:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "a.do.do-task.btn.btn-sm.btn-primary.btn-block[data-task-id]")))
Using XPATH:
element_list = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//a[#class='do do-task btn btn-sm btn-primary btn-block' and #data-task-id]")))
Next, you can access each list item and invoke click() on each of them one by one.
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
If this css
a.do.do-task.btn.btn-sm.btn-primary.btn-block
represent them, then you can use the below code :
for a in driver.find_elements_by_css_selector("a.do.do-task.btn.btn-sm.btn-primary.btn-block"):
a.click()
You may have to scroll to each element or put some delay before click, this depends on your use case and your automation strategy.

Trouble selecting google one box tab in selenium

going to https://www.google.com/search?q=tennis a google one box comes up for scores, I'm trying to click on the tabs and (Women's Singles, Men's Doubles etc) and having no luck. I've tried all methods, xpath, classname, partial link text, css selector.
any help would be appreciated!!!
The desired element is a JavaScript enabled 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:
Clicking Women's Singles:
driver.get('https://www.google.com/search?q=tennis')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#jsslot]//span//span[contains(., 'Women') and contains(., 'Singles')]"))).click()
Clicking Men's Doubles:
driver.get('https://www.google.com/search?q=tennis')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#jsslot]//span//span[contains(., 'Men') and contains(., 'Doubles')]"))).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 always use this method to set the XPath in Selenium based Python projects.
Click F12 after opening the link
In Elements, Click Ctrl + F. Now search some unique term in the page. I gave Men's Singles and this snippet came up.
<div class="SVWlSe FZzi2e" jsslot="">
<span jsslot="">
<div aria-controls="_dtAkYIqaMLraz7sPnrW54A435_0"
id="_dtAkYIqaMLraz7sPnrW54A434_0">
<span>
<span>Men's Singles</span>
</span>
</div>
</span>
</div>
Now right-click the snippet to get the XPath format or use the XPath Syntax. Here, the XPath is //div[#class='SVWlSe FZzi2e']. To find whether the attribute value is unique, I would suggest you to search within elements again with the value.
Notice that I used the class attribute instead of the aria-controls and id attributes since id and aria-controls changed over time.

AttributeError: 'list' object has no attribute 'click' error clicking on an element using Selenium and Python

I need help this is my script:
# Imports
from selenium import webdriver
url = "https://sjc.cloudsigma.com/ui/4.0/login"
d = webdriver.Chrome()
d.get(url)
escolhe = d.find_elements_by_xpath('//*[#id="trynow"]')
escolhe.click()
and this is what the html looks like:
<button id="trynow" class="btn g-recaptcha block full-width m-b dwse btn-warning" ng-class="{'btn-danger': instantAccess=='Error', 'btn-success': instantAccess=='Success', 'btn-warning': instantAccess=='Working', 'btn-warning': (instantAccess!='Working' && instantAccess!='Success' && instantAccess!='Error')}" data-ng-disabled="instantAccess=='Working' || instantAccess=='Success' || instantAccess=='Error'" analytics-on="click" analytics-event="Guest logged in" analytics-category="Guest logged in" analytics-label="Guest logged in" data-sitekey="6Lcf-2MUAAAAAKG8gJ-MTkwwwVw1XGshqh8mRq25" data-callback="onTryNow" data-size="invisible">
<span name="btn-warning" class="default " style="font-size: 20px;">
<i class="fa fa-thumbs-up"></i> Instant access
<p style="font-size: 9px;font-style: italic;margin: 2px 0px;" class="ng-binding">No credit card is required</p>
</span>
<span name="btn-warning" class="working hide" disabled=""><i class="fa fa-spinner fa-spin"></i> Instant access...</span> <span name="btn-success" class="success hide" disabled=""><i class="fa fa-spinner fa-spin"></i> Entrar na sessão</span> <span name="btn-danger" class="error hide" disabled=""><i class="fa fa-exclamation-circle"></i> Erro</span>
</button>
I need help because whenever I put xpath this error:
AttributeError: 'list' object has no attribute 'click'
Find element not elements.
from selenium import webdriver
import time
url = "https://sjc.cloudsigma.com/ui/4.0/login"
d = webdriver.Chrome(executable_path='C:/bin/chromedriver.exe')
d.get(url)
time.sleep(5) #Wait a little for page to load.
escolhe = d.find_element_by_xpath('//*[#id="trynow"]/span[1]')
escolhe.click()
You have to write element not elements --> Even if there is only one item it will treat it as a list. You have to modify like this:
from selenium import webdriver
url = "https://sjc.cloudsigma.com/ui/4.0/login"
d = webdriver.Chrome()
d.get(url)
escolhe = d.find_element_by_xpath('//*[#id="trynow"]')
escolhe.click()
This error message...
AttributeError: 'list' object has no attribute 'click'
...implies that you have tried to invoke click() method on a list element where as click() is invoked on a WebElement.
Solution
To click on the desired element you can use either of the following Locator Strategies:
Using css_selector:
driver.find_element_by_css_selector("button#trynow").click()
Using xpath:
driver.find_element_by_xpath("//button[#id='trynow']").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:
driver.get('https://sjc.cloudsigma.com/ui/4.0/login')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#trynow"))).click()
Using XPATH:
driver.get('https://sjc.cloudsigma.com/ui/4.0/login')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[#id='trynow']"))).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
Your code returns a list of webelements. Webelements can be clicked on. You either need to select one webelement of the list by index, such as:
escolhe = d.find_elements_by_xpath('//*[#id="trynow"]')
escolhe[0].click() #first element
or use find_element_by_xpath to return the first element that matches your xpath locator:
escolhe = d.find_element_by_xpath('//*[#id="trynow"]')
escolhe.click()
I'd also suggest using find_element_by_id if you're using an id locator strategy:
escolhe = d.find_element_by_id('trynow')
escolhe.click()

Cannot click tag <a> with <i class> selenium python

I want to make a simple attendance automation with python for my college, I make this for ease, because this took so many button to click but, I cannot click the tag <a> with tag <i> inside it.
<a href="http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add"
class="btn btn-sm green-meadow btn-add-data" id="btn-add-wizard">
<i class="fa fa-plus"></i> Absen</a>
I use this wait function, and still not directed into that link. It shows nothing on result. Process finished with exit code 0
wait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,"//a[contains(#href, ""'http://siakad.polinema.ac.id/mahasiswa/tr_absensi/add')]"))).click()
I tried using ID and LINK_TEXT and still showing Process finished with exit code 0.
Thank you, apologize for my english. Enghlish is not my main language. If you need more information about my question, please let me know.
To click on the element with text as Absen you have 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, "a.btn.btn-sm.green-meadow.btn-add-data#btn-add-wizard[href$='id/mahasiswa/tr_absensi/add']"))).click()
Using XPATH:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[#class='btn btn-sm green-meadow btn-add-data' and #id='btn-add-wizard'][contains(#href, 'id/mahasiswa/tr_absensi/add')]"))).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

Return UID attribute value using Selenium Python

I have a site that looks like this and I want to extract the contents of the uid field using firefox + selenium + python. There is only 1 UID per page.
<div class= "parent" >
<div class="module profile" dcp="1" uid="90">
...
...
</div>
</div>
To make it concrete see the following:
<div class= "parent" >
<div class="module profile" dcp="1" uid="[RETURN THIS]">
...
...
</div>
</div>
I've tried several techniques in selenium including using
browser.find_elements_by_name
browser.find_elements_by_xpath
browser.find_elements_by_class_name
browser.find_elements_by_css_selector
But none of them are able to return the UID. I either get an empty set or I only get the class (i.e. the entire module class rather than the attributes inside the DIV).
I saw some folks recommend the get_attribute selector but I was unsuccessful in applying it to this use case.
Any guidance would be appreciated, thanks.
To extract the value of the attribute uid i.e. the text 90 you can use either of the Locator Strategies:
css_selector:
myText = browser.find_element_by_css_selector("div.parent>div.module.profile").get_attribute("uid")
xpath:
myText = browser.find_element_by_xpath("//div[#class='parent']/div[#class='module profile']").get_attribute("uid")
However it seems the attribute uid i.e. the text 90 is a dynamic element so you have to induce WebDriverWait for the element to be visible and you can use either of the following solutions:
css_selector:
myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.parent>div.module.profile"))).get_attribute("uid")
xpath:
myText = WebDriverWait(browser, 20).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='parent']/div[#class='module profile']"))).get_attribute("uid")
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