Select class with selenium python - 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.

Related

Getting the text of a paragraph element using Selenium

`<div id="businessCategory12">`
`<p style="margin-top: 0px;line-height:80%;margin-left:5px;font-weight: bold;color:#00004C">Business Types</p>`
`<p style="margin-top: 0px;line-height:80%;margin-left:15px;font-weight: bold;"> Minority Owned Business</p>`
`<p style="margin-top: 0px;line-height:80%;margin-left:15px;"> Black American Owned</p>`
`</div>``
I am working on a webscraping tool for a client. I need to get the text from the third paragraph above using selenium (python) but I am having a lot of trouble. The text should be "Black American Owned". I have tried the following but it keeps giving me a null value. What am I doing wrong here?
Any help or other way to get the text would be greatly greatly appreciated!
`minority = driver.find_element_by_xpath("//*[#id='businessCategory12']/p[3]")`
`minority_owned = minority.text`
Possibly the node is hidden try with textContent instead of text
minority = driver.find_element_by_xpath("//*[#id='businessCategory12']/p[3]")
minority_owned = minority.get_attribute("textContent")
<div id="businessCategory12">
<p style="margin-top: 0px;line-height:80%;margin-left:5px;font-weight: bold;color:#00004C">Business Types</p>
<p style="margin-top: 0px;line-height:80%;margin-left:15px;font-weight: bold;">Minority Owned Business</p>
<p style="margin-top: 0px;line-height:80%;margin-left:15px;">Black American Owned</p>
</div>
Just try:
//p[3]/text()
Here is a good site to play around xpath:
https://scrapinghub.github.io/xpath-playground/

Python find value of attribute with lxml

How am i gonna be able to get the value of data-sku with from lxml import html
<button data-e2e="product-size" type="button" class="btn btn-default "
data-price="DKK 1,750.00"
data-sku="050226_jdsportsdk.006458"
data-brand="Nike"
title="Vælg størrelse 42"
>
42
<span class="fulfilment-notice-html hide">
That is the html ^^.
i tried xpath('//button[#data-e2e="product-size"]//#data-sku()')
but doesnt work
I want to get the value of data-sku, so that would be "050226_jdsportsdk.006458". I do not know that its going to be "050226_jdsportsdk.006458", so thats why i need some way of scraping that part.
Thanks
I found out a way to fix it!
i had () after the #data-sku, and also a double //,
the right answer is xpath('//button[#data-e2e="product-size"]/#data-sku')

I am trying to webscrape to pycharm using selenium

This is the HTML text that gives the output -£0.03.
I`d like to find this value by xpath and print the output to pycharm console.
<div class="pnl runner-info-elem below-runner-info" ng-if="ctrl.vm.events.shouldShowPnl(runner)" ng-class="{'below-runner-info': ctrl.vm.data.displayRaceCardInfo}">
<!---->
<mv-runner-pnl ng-repeat="(type, pnl) in ctrl.vm.data.pnl[runner.key] track by type" type="actual" values="pnl.values" separator="comma" formatter="::pnl.formatter">
<!-- PnL -->
<div class="runner-elem-pnl actual-pnl">
<span class="prefix"></span>
<span class="pnl-value-container">
<span class="pnl-value negative">-£0.03</span>
</span>
<span class="pnl-value-container hidden"><span>
I have tried the following:
driver.find_element_by_xpath('//*[#id="main-wrapper"]/div/div[3]/div/div[2]/div/div[1]/div[3]/div/div[1]/div/bf-main-market/bf-main-marketview/div/div[2]/bf-marketview-runners-list[2]/div/div/div/table/tbody/tr[1]/td[1]/div/div[3]').get_attribute(pnl.value)
print(pnl.value)
My result is:
:NameError: name 'pnl' is not defined
Is this even possible? If not by xpath then by any other means?
As I said I am totally new to this and trying to learn from YouTube tutorials.
With the provided html, you could use:
el= driver.find_element_by_xpath("//div/mv-runner-pnl/div[#class='runner-elem-pnl actual-pnl']/span[#class='pnl-value-container']/span[#class='pnl-value negative']")
print(el.text)

How to click a button from 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()

Python 3.5 + Selenium Scrape. Is there anyway to select <a><a/> tags?

So I'm very new to python and selenium. I'm writting an scraper to take some balances and download a txt file. So far I've managed to grab the account balances but downloading the txt files have proven to be a difficult task.
This is a sample of the html
<td>
<div id="expoDato_msdd" class="dd noImprimible" style="width: 135px">
<div id="expoDato_title123" class="ddTitle">
<span id="expoDato_arrow" class="arrow" style="background-position: 0pt 0pt"></span>
<span id="expoDato_titletext" class="textTitle">Exportar Datos</span>
</div>
<div id="expoDato_child" class="ddChild" style="width: 133px; z-index: 50">
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=txt">txt</a>
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=pdf">PDF</a>
<a class="enabled" href="/CCOLEmpresasCartolaHistoricaWEB/exportarDatos.do;jsessionid=9817239879882871987129837882222R?tipoExportacion=excel">Excel</a>
<a class="modal" href="#info_formatos">Información Formatos</a>
</div>
</div>
I need to click on the fisrt "a" class=enabled. But i just can't manage to get there by xpath, class or whatever really. Here is the last thing i tried.
#Descarga de Archivos
ddmenu2 = driver.find_element_by_id("expoDato_child")
ddmenu2.find_element_by_css_selector("txt").click()
This is more of the stuff i've already tryed
#TXT = driver.select
#TXT.send_keys(Keys.RETURN)
#ddmenu2 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]")
#Descarga = ddmenu2.find_element_by_visible_text("txt")
#Descarga.send_keys(Keys.RETURN)
Please i would apreciate your help.
Ps:English is not my native language, so i'm sorry for any confusion.
EDIT:
This was the approach that worked, I'll try your other suggetions to make a more neat code. Also it will only work if the mouse pointer is over the browser windows, it doesn't matter where.
ddmenu2a = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[1]").click()
ddmenu2b = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]")
ddmenu2c = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div/div/form/table/tbody/tr[2]/td/div[2]/table/tbody/tr/td[4]/div/div[2]/a[1]").click()
Pretty much brute force, but im getting to like python scripting.
Or simply use CSS to match on the href:
driver.find_element_by_css_selector("div#expoDato_child a.enabled[href*='txt']")
You can get all anchor elements like this:
a_list = driver.find_elements_by_tag_name('a')
this will return a list of elements. you can click on each element:
for a in a_list:
a.click()
driver.back()
or try xpath for each anchor element:
a1 = driver.find_element_by_xpath('//a[#class="enabled"][1]')
a2 = driver.find_element_by_xpath('//a[#class="enabled"][2]')
a3 = driver.find_element_by_xpath('//a[#class="enabled"][3]')
Please let me know if this was helpful
you can directly reach the elements by xpath via text:
driver.find_element_by_xpath("//*[#id='expoDato_child' and contains(., 'txt')]").click()
driver.find_element_by_xpath("//*[#id='expoDato_child' and contains(., 'PDF')]").click()
...
If there is a public link for the page in question that would be helpful.
However, generally, I can think of two methods for this:
If you can discover the direct link you can extract the link text and use pythons' urllib and download the file directly.
or
Use use Seleniums' click function and have it click on the link in the page.
A quick search resulted thusly:
downloading-file-using-selenium

Categories