Python selenium webdriver select href select partial text - python

I'm using Python 3.8.5 on Ubuntu 20.04. Using selenium webdriver on chrome, I want to download the attachment by specifying the licenceId number (1467250) which is included in this element:
<a xmlns="http://www.w3.org/1999/xhtml" href="#" onclick="if(typeof jsfcljs == 'function'){jsfcljs(document.forms['myApplicationsResultsForm'],'myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,myApplicationsResultsForm:searchResultsTable:0:j_id339:0:j_id340,licenceId,1467250,statusHistoryId,2600790,fileName,ROL_1467250_20200817-142839.pdf,attachmentType,ROL','');}return false" class="pageLink"><img src="/oplinc2/images/pdf.jpg" alt="ROL_1467250_20200817-142839.pdf" height="24" style="border-width: 0px;" title="ROL_1467250_20200817-142839.pdf" width="24" /></a>
I am able to download this link by clicking on the css_selector:
pdf = driver.find_element_by_css_selector('#myApplicationsResultsForm\:searchResultsTable\:0\:j_id335 > a')
pdf.click()
Am I able to use partial text within the element to locate and download attachment eg. licenceID, 1467250? There are many of these attachments. I tried the partial text example from the docs but this didn't work for me:
>>> driver.find_element_by_partial_link_text('1467250')
NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"1467250"}
Edit
This question is similar to #Ajay link to this solution except this element has slightly different href. Still not sure how to access onclick

try this
links = driver.find_elements_by_partial_link_text('https://websites.com/activation.php?a=act')
for link in links:
print(link.get_attribute("href"))

driver.find_element_by_xpath('//*[contains(#onclick, "1467250")]')
Replacing the a with * finds the element.

Related

Get text from div using Selenium and Python

Situation
I'm using Selenium and Python to extract info from a page
Here is the div I want to extract from:
I want to extract the "Registre-se" and the "Login" text.
My code
from selenium import webdriver
url = 'https://www.bet365.com/#/AVR/B146/R^1'
driver = webdriver.Chrome()
driver.get(url.format(q=''))
elements = driver.find_elements_by_class_name('hm-MainHeaderRHSLoggedOutNarrow_Join ')
for e in elements:
print(e.text)
elements = driver.find_elements_by_class_name('hm-MainHeaderRHSLoggedOutNarrow_Login ')
for e in elements:
print(e.text)
Problem
My code don't send any output.
HTML
<div class="hm-MainHeaderRHSLoggedOutNarrow_Join ">Registre-se</div>
<div class="hm-MainHeaderRHSLoggedOutNarrow_Login " style="">Login</div>
By looking this HTML
<div class="hm-MainHeaderRHSLoggedOutNarrow_Join ">Registre-se</div>
<div class="hm-MainHeaderRHSLoggedOutNarrow_Login " style="">Login</div>
and your code, which looks okay to me, except that part you are using find_elements for a single web element.
and by reading this comment
The class name "hm-MainHeaderRHSLoggedOutMed_Login " only appear in
the inspect of the website, but not in the page source. What it's
supposed to do now?
It is clear that the element is in either iframe or shadow root.
Cause page_source does not look for iframe.
Please check if it is in iframe, then you'd have to switch to iframe first and then you can use the code that you have.
switch it like this :
driver.switch_to.frame(driver.find_element_by_xpath('xpath here'))

Problem in scraping link(href) using selenium; href="#"

I'm a amateur at using python, and I'm trying to scrape the url from the html below using selenium.
<a class="" href="#" style="text-decoration: none; color: #1b1b1b;" onclick="toDetailOrUrl(event, '1641438','')">[안내] 빗썸 - 빗썸 글로벌 간 간편 가상자산 이동 서비스 종료 안내</a>
In ordinary case, the link url i want to get is in just beside 'href=', but there is just "#" in that html.
When i run the code below that is usual way to using selenium to scrape the given html, it returns a https://cafe.bithumb.com/view/boards/43. But is just what i entered in 'driver.get()', and i don't want.
url = "https://cafe.bithumb.com/view/boards/43"
driver=webdriver.Chrome('chromedriver.exe')
driver.get(url)
driver.implicitly_wait(30)
bo =driver.find_element_by_xpath("//tbody[1]/tr[#style='cursor:pointer;border-top:1px solid #dee2e6;background-color: white']/td[2]/a")
print(bo.get_attribute('href'))
What i want is https://cafe.bithumb.com/view/board-contents/1641438. You can get this url when you click a item corresponding with the xpath i wrote above.
I want this url using selenium or other programmatic ways, no need to open a chrome and enter the url in addressbar, and click using mouse... like that.
good
You can use,
bo.click()
in order to click the element you want (I assumed you want to click bo)
print(driver.execute_script('return arguments[0].getAttribute("href")',bo))
selenium , bo.get_attribute('href') is actually doing document.getElementById("somelocaator").href which returns full href , as '#' indicates current page you get current URL you provided in get()
If you just need # you can use the execute_script

How do I find HTML Elements not in the page source using Selenium?

So I'm trying to find this <ul> tag I found using inspect element on chrome:
<ul class = "jobs-search-results__list artdeco-list" itemtype="http://schema.org/ItemList"></ul>
This is what I tried in Python:
ul = driver.find_element_by_class_name("jobs-search-results__list artdeco-list")
Which should return the <ul> tag.
Instead I get this error:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element:{"method":"class","selector":"jobs-search-results__list artdeco-list"}
I get the same error whether I use a tag/xpath/absolutepath selector.
Then I find out this element is not on the HTML page source, and so selenium can't find it.
HTML Source (pastebin)
How do I go about finding this element if its not on the page source?
The class of ul element that you are trying to get is changing while accessing site using Selenium. For this use the xpath as
//ul[contains(#class,'jobs-search__results')]
Now you can find ul element as
ul = driver.find_element_by_xpath("//ul[contains(#class,'jobs-search__results')]")

How to extract the text through a href using Selenium and Python

I looked on every Posts but didnt get the solution i want.
browser.find_element_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a")
pic = browser.find_elements_by_xpath("//*/section/main/div/div[3]/article/div[1]/div/div[1]/div[1]/a").get(title)
print(title)
Thats my Code just now but I Need exactly this:
<div class="eLAPa"><div class="KL4Bh"><img class="FFVAD" decoding="auto" sizes="281.671875px" srcset="https://scontent-frx5-1.cdninstagram.com/vp/38a5fdde34937b2b3e4e600da56c46b5/5C0D05B3/t51.2885-15/e15/s150x150/46276509_388170972011907_7609813800358803282_n.jpg 150w,https://scontent-frx5-1.cdninstagram.com/vp/74b222ca252a488bb5eb110347283c3b/5C0CB7F9/t51.2885-15/e15/s240x240/46276509_388170972011907_7609813800358803282_n.jpg 240w,https://scontent-frx5-1.cdninstagram.com/vp/af2eff79d9c38b0f97a763041e3a99e5/5C0D10C3/t51.2885-15/e15/s320x320/46276509_388170972011907_7609813800358803282_n.jpg 320w,https://scontent-frx5-1.cdninstagram.com/vp/bad123f6264636de35489b7d8dc2cbed/5C0CC199/t51.2885-15/e15/s480x480/46276509_388170972011907_7609813800358803282_n.jpg 480w,https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg 640w" src="https://scontent-frx5-1.cdninstagram.com/vp/93a4e36addf453f754e4caba706fe93a/5C0CC82C/t51.2885-15/e15/s640x640/46276509_388170972011907_7609813800358803282_n.jpg" style=""></div><div class="_9AhH0"></div></div><div class="u7YqG"><div class="Byj2F"><span class="glyphsSpriteVideo_large u-__7" aria-label="Video"></span></div></div>
a href and then Comes a link which i need
To extract the text Video you can use either of the following solutions:
css_selector:
driver.find_element_by_css_selector("a[href='/p/BrDudIUBuNr/'] span.glyphsSpriteVideo_large").get_attribute("aria-label")
xpath:
driver.find_element_by_xpath("//a[#href='/p/BrDudIUBuNr/']//span[contains(#class,'glyphsSpriteVideo_large')]").get_attribute("aria-label")

Python Selenium: Can't find an element on page

I'm pretty new to using python in selenium.
I have been trying to select a button on my web page. Here is the piece of HTML that appears after inspecting the element of the button:
<a class="btn col-xs-3 nav-btns" ui-sref="salt.dashboard.reports.minions" href="/dashboard/reports/minions/">
<span class="ssIcons-icon_reports salt-icon-3x ng-scope active" bs-tooltip="" data-title="Reports" container="body" placement="bottom" animation="none" data-trigger="hover" ng-class="{'active': state.current.name =='salt.dashboard.reports' … || state.current.name =='salt.dashboard.reports.minions'}">
::before
</span>
</a>
I have tried everything I can think of. Here are some of the things that I have tried:
element = driver.find_element_by_class_name("btncol-xs-3")
element = driver.find_element_by_name("Reports")
element = driver.find_element_by_id("Reports")
the error that I keep getting is:
selenium.common.exceptions.NoSuchElementException: Message: Unable to
locate element: {"method":"class
name","selector":"salt.dashboard.reports"} Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/driver-component.js:10299)
at FirefoxDriver.prototype.findElement (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/driver-component.js:10308)
at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12282)
at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12287)
at DelayedCommand.prototype.execute/< (file:///tmp/tmpoRPJXA/extensions/fxdriver#googlecode.com/components/command-processor.js:12229)
root#chris-salt:/home/chris/Documents/projects/python-selenium#
Find the element by data-title:
driver.find_element_by_css_selector("span[data-title=Reports]")
Or, if you need to get to the a tag:
driver.find_element_by_xpath("//a[span/#data-title = 'Reports']")
Chris,
The span that you pasted doesn't has an attribute named id.
Also, your class selector is too wide, i'd suggest using a more explicit path following the dom structure. Bare in mind that there may be multiple elements that have that class name.
Also, you are trying to find by the attribute name, which you don't have in that element.
Finally, it seems that you might be using angular. Does the input that you are looking for is created with javascript dinamically ?
And also, why are you using root to do this tests ?
Before doing the asserts, can you store the resulting html and manually checking that you indeed have that element?.

Categories