How to click a button from Python - python

I am using Anaconda2, Jupiter, and Chrome browser.
I am writing below code which is running successfully but now I want to click on button.
<a href="#" action="exportSelected" class="btn btn-default">
<i class="glyphicon glyphicon-download"></i> Export
</a>
What should I write in Python to access this?
find_element_by_partial_link_text('btn btn-default').click()
It's giving an error.

"btn btn-default" is not a link text, but class names.
You can use one of below solutions:
Locate by exact link text:
find_element_by_link_text('Export').click()
Locate by compound class name:
find_element_by_css_selector('a.btn.btn-default').click()
Locate by action attribute
find_element_by_css_selector('a[action="exportSelected"]').click()

Thank you all replying me and suggesting pyhthon doc. appreciate it.
So i have used very simple lines to get my job done.
driver.find_element_by_xpath('copy your xpath').
which is very simple
driver.find_element_by_xpath('//[#id="app_content"]/div[2]/div/div/section/div/div[2]/div[1]/div[1]/div/div[2]/a[1]').click()
driver.implicitly_wait(5)
driver.find_element_by_xpath('//[#id="export_button"]').click()

Related

Selenium Unable to Locate Element, but it surely exists

EDIT FROM FUTURE, MAY BE YOUR SOLUTION: The page that I was trying to deal with opens on a new tab which I've missed to notice. My driver was trying to find the element on the very first tab, that is why it was unable to locate element.
--
--
--
I tried all solutions on Stackoverflow, however still got the same problem.
I am trying to select a specific option from a dropbox. It looks like this:
click to see the dropbox.
The HTML that belongs to this dropbox is as follows:
<td ct="GLC" lsdata="{1:'STANDARD'}" id="WD18" class="lsContainerCell lsGLCTopVAlign lsContainerCellVAlign--top urLayoutDefault--grid" valign="TOP" align="left" style="white-space:normal;"><span id="WD19-r" class="lsField lsField--standalone lsField--explicitwidth lsField--hasellipsis lsField--hashelp lsField--list lsField--focus lsField--interactiontarget" aria-live="assertive" style=""><input id="WD19" ct="CB" lsdata="{2:'WD1A',3:'10',4:'2021\x2f2022',27:'WD17'}" lsevents="{Select:[{ResponseData:'delta',EnqueueCardinality:'single'},{}]}" type="text" autocomplete="off" tabindex="0" ti="0" class="lsField__input" readonly="" value="2021/2022" role="combobox" aria-haspopup="listbox" aria-readonly="false" aria-controls="WD1A" aria-expanded="false" aria-autocomplete="list" style="width:150px;" title=""><span id="WD19-btn" class="lsField__help" tabindex="-1" ti="-1" aria-hidden="true" role="button"></span></span></td>
Nothing I have tried did work because Selenium can not locate element. I've tried it with By.ID and By.XPATH. Last thing I have tried is this:
driver.find_element(By.XPATH, "/html/body/table/tbody/tr/td/div/div[1]/span/span[1]/table/tbody/tr[2]/td/table/tbody/tr[1]/td[2]")
But I still got the same error for the thousandth time:
Message: no such element: Unable to locate element
Thanks in advance.
Try this Xpath, avoid absolute path (click element, get xpath -_-)
driver.find_element(By.XPATH, "//td[#ct='GLC']")

Select class with selenium python

Hi I'm trying to automate a process with selenium, python and GLPI, but recently I spend a lot o f time trying to select a user of a menu bar,I already did tests with linkText,cssSelector,xpath but none works for me, Maybe I'll be doing it wrong, I appreciate your help.
driver.find_element_by_xpath("//a[contains(#href,'javascript:void(0)') and contains(.,'select2-choice')]").click()
driver.find_element_by_css_selector("a[href*='select2-container select2-container-active select2-dropdown-open']").click()
driver.find_element_by_link_text("javascript:void(0)").click()
https://ibb.co/RbrXDrv that is the code source
<div class="select2-container select2-container-active select2-dropdown-open" id="s2id_dropdown__users_id_requester722037505" style="width: 80%;"> <span class="select2-chosen" id="select2-chosen-4">-----</span><abbr class="select2-search-choice-close"></abbr> <span class="select2-arrow" role="presentation"><b role="presentation"></b></span><label for="s2id_autogen4" class="select2-offscreen"></label><input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-4" id="s2id_autogen4" disabled=""></div>
thanks
Your xpath is not correct.
Try this:
driver.find_element_by_xpath("//a[#href='javascript:void(0)' and #class='select2-choice']").click()
BTW, it would be better to provide url or html, not picture.

How can I access bulk-edit button "bulkedit_all"? Python / Selenium

I am trying to automate JIRA tasks but struggling to access bulkedit option after JQL filter. After accessing the correct sceen I am stuck at this point:
enter image description here
HTML code:
<div class="aui-list">
<h5>Bulk Change:</h5>
<ul class="aui-list-sectionaui-first aui-last">
<li class="aui-list-item active">
<a class="aui-list-item-link" id="bulkedit_all" href="/secure/views/bulkedit/BulkEdit1!default.jspa?reset=true&tempMax=4">all 4 issue(s)</a>
</li>
</ul>
</div>
My Python code:
bulkDropdown = browser.find_elements_by_xpath("//div[#class='aui-list']//aui-list[#class='aui-list-item.active']").click()
Try the following xpath -
bulkDropdown = browser.find_elements_by_xpath("//li/a[#id='bulkedit_all']").click()
The link you want has an ID, you should use that unless you find that it's not unique on the page.
browser.find_element_by_id("bulkedit_all").click()
You will likely need to add a wait for clickable since from the screenshot it looks like a popup or tooltip of some kind. See the docs for more info on the different waits available.

Python Selenium Scrape with dropdown

I am using python and selenium to scrape a website. However I am having issues selecting an element from a dropdown list. When I run this script on windows in the python IDE I can get my script to work:
listElement = driver.find_element_by_id('header-transactionTypeOptions')
transElement = driver.find_element_by_id('container-primary-4-transactionTypeOptions')
listElement.click()
time.sleep(2)
transElement.click()
<input id="header-transactionTypeOptions" class="jpui input header focus-on-header wrap right text-float-left " type="button" aria-disabled="false" aria-expanded="false" aria-label=" SHOWING:: Activity since last statement" value="Activity since last statement">
However, when I run this on my ubuntu instance with pyvirtualdisplay it tells me it cannot locate the element. In my research for using selenium with a drop down - the suggested answers are using "Select". However there is no "Select ID" on the elements on this webpage.
I have include the elements below, as other options to reference in trying to find an answer:
<button id="iconButton-transactionTypeOptions" type="button" class="jpui input-icon icon text-overflow" aria-hidden="true" tabindex="-1"><span class="jpui expanddown icon input-icon hasError"></span></button>
<span class="primary" id="container-primary-1-transactionTypeOptions">Jul 21, 2016 statement </span>

Selenium find element and click on it

I'm trying to get Selenium to click on View All Companies button, but i'm not sure what am I doing wrong. It returns no element found
html code
<div class="screener-toggles">
<div class="buttons">
<span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span>
<span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span>
<span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span>
</div>
</div>
python code I wrote
element1 = driver.find_elements_by_class_name('View All Companies')
element1.click()
# I have tried all-companies instead of View All Companies as well. But still doesn't work
Should I not be using find_elements_by_class_name?
Any advice on what I am doing wrong is greatly appreciated!
try xpath: "//span[contains(text(),'View All Companies')]"
View All Companies is text, not the class. Try looking by text with css_selector or xpath
element1 = find_element_by_css_selector('span:contains("View All Companies")')
element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]')
Or by the data-name attribute which contains all-companies
element1 = find_element_by_css_selector('span[data-name*="all-companies"]')
Yes, you should not use the find_elements_by_class_name instead of use find_element_by_class_name.
find_elements_by_class_name is used when your expecting your locator to return more than 1 element. for a specific element use only find_element_by_class_name.
Another thing is I am not able to see any class name as View All Companies in your HTML code. Please look into your HTML and select classname or other locator carefully
Hope it will help you

Categories