I have following piece of html:
<p class="attrs"><span>foo:</span> <strong>foo</strong></p>
<p class="attrs"><span>bar:</span> <strong>bar</strong></p>
<p class="attrs"><span>foo2:</span> <strong></strong></p>
<p class="attrs"><span>description:</span> <strong>description body</strong></p>
<p class="attrs"><span>another foo:</span> <strong>foooo</strong></p>
I would like to get description body using splinter. I've managed to get a list of p using
browser.find_by_css("p.attrs")
xpath = '//p[#class="attrs"]/span[text()="description:"]/following-sibling::strong'
description = browser.find_by_xpath(xpath).first.text
Would you be able to get the description using find_by_tag?
Find by Tag
browser.find_by_tag('span')
Then go iterate through all 'span' tags and look for the value of 'description'. I used the documentation here
You may be able to acomplish using this code, if you want to try a different approach with the selenium library:
import selenium
from selenium import webdriver
driver = webdriver.Chrome('PATH_LOCATION_TO_CHROME_DRIVER')
driver.find_elements_by_class_name("attrs")
Hope this helps! replace PATH_LOCATION_TO_CHROME_DRIVER --- with the location of your chrome driver, if you google it should be first or second link to download and then place that download inside your Python's project folder.
Related
im trying to get all link text from a tag p and with a specific class. Then create a loop to find all other similar elements.
how it looks
so far i am using this :
the value i want is in
<div class='some other class'>
<p class='machine-name install-info-entry x-hidden-focus'> text i want
</p> ==$0
installations = browser.find_elements_by_css_selector('p.machine-name.install-info-entry.x-hidden-focus')
any help is appreciated. thanks.
You can just use .text
installations = browser.find_elements_by_css_selector('p.machine-name.install-info-entry.x-hidden-focus')
for installation in installations:
print(installation.text)
Note that installations is a list of web elements, whereas installation is just a web element from the list.
UPDATE1:
If you want to extract the attribute from a web element, then you can follow this code:
print(installation.get_attribute("attribute name"))
You should pass your desired attribute name in get_attribute method.
You can read innerHTML attribute to get source of the content of the element or outerHTML for source with the current element.
installation.get_attribute('innerHTML')
Hope this will be helpful.
I'm trying to use Pythons Selenium module to click on an element whose link has the text "xlsx" at the end of it. Below is the code I'm using and the details of the element. Can someone please see why Python is unable to find this element?
driver.find_element_by_partial_link_text('xlsx').click()
Here is the element details:
<a name="URL$2" id="URL$2" ptlinktgt="pt_new" tabindex="43" onfocus="doFocus_win0(this,false,true);" href="http:******/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx" onclick="window.open('http:********/HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx','','');cancelBubble(event);return false;" class="PSHYPERLINK">HSC8_CNTRCT_ITEMS_IMPRVD-1479218.xlsx</a>
I had to remove some parts of the URL for confidentiality purposes, however, it should not impact the answering of the question.
Thanks.
Thanks for the replies. Turns out, as #Andersson mentioned, the window was in a different frame.
I solved the problem using the following code before the find_element: driver.switch_to.frame('ptModFrame_0').
You can use a CSS selector:
driver.find_element_by_css_selector("a[href*='xlsx']")
If the element still cannot be located, I would suggest using a wait statement, to ensure that the element is visible, before you interact with it.
Please try:
driver.find_element_by_xpath(".//a[contains(#href,'xlsx')]").
You can grab it by class name (class name = PSHYPERLINK).
This should work:
import selenium
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
url = ''
driver = webdriver.Chrome('/path/to/chromedriver/executable')
if __name__=='__main__':
driver.get(url)
time.sleep(3)
driver.find_element_by_class_name('PSHYPERLINK').click()
When finding the attribute, make sure to use a singular '"element". Like:
driver.find_element_by_class_name('PSHYPERLINK').click()
not:
driver.find_elements_by_class_name('PSHYPERLINK').click()
Hope this helps.
I'm using Selenium with python to make a spider.
A part of the web page is like this:
text - <span class="sr-keyword">name</span> text
I need to find the href and click.
I've tried as below:
target = driver.find_element_by_class_name('_j_search_link')
target.click()
target is not None but it doesn't seem that it could be clicked because after target.click(), nothing happened.
Also, I've read this question: Click on hyperlink using Selenium Webdriver
But it can't help me because in my case, there is a <span class>, not just a simple text Google.
You can look for an element with class _j_search_link that contains text Wat Chedi Luang
driver.find_element_by_xpath('//a[#class="_j_search_link" and contains(., "Wat Chedi Luang")]')
driver.find_elements_by_partial_link_text("Wat Chedi Luang").click()
I think you didn't find right element. Use CSS or Xpath to target this element then click it, like:
//a[#class='_j_search_link']
or
//a[#class='_j_search_link']/span[#class='sr-keyword']
I'm trying to extract the links on this html page:
<div class="listbox">
<div class="mainbox" onclick="www.abc.com">
I've tried using:
//div[#class="listbox"]/a/text()
//div/onclick/text()
but they return an empty list.
Such XPath must work for you.
/div/div/#onclick
or more precise
/div[#class="listbox"]/div[#class="mainbox"]/#onclick
In your case you can obtain the link via using Selenium and the getAttribute method.
First find the element (or elements and then loop) that have the links inside their onclick attributes, then just get them via getAttribute:
Selenium + Java:
String link = driver.findElement(By.className("mainbox")).getAttribute("onclick");
Selenium + Python:
I'm no python guy, but it should work like this:
link = driver.find_element_by_class_name("mainbox")).get_attribute("onclick");
HTML:
<g id="OpenLayers.Layer.Vector_101_vroot">
<image id="OpenLayers.Geometry.Point_259_status"..></image>
So the page generates the above and the number section of the Id is different on each load.
How do I located them, or even a group of them that match the pattern using selenium and python?
Use Xpaths like below:
//g[contains(#id, 'OpenLayers.Layer.Vector')]
//image[contains(#id, 'OpenLayers.Geometry.Point')]
Hope if helps!
according to this answer, you can use css3 substring matching attribute selector.
the following code clicks an element which contains OpenLayers.Layer.Vector in id attribute.
Python
from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://localhost:1111/')
browser.find_element_by_css_selector('[id*="OpenLayers.Layer.Vector"]').click()
HTML (which is displayed in http://localhost:1111/)
<button id="OpenLayers.Layer.Vector_123" onclick="alert(1);return false">xxx</button>
no need for and pattern matching, you can use this module here called "beautiful soup" with some easy documentation here.
for example to get all tags with id="OpenLayers.Layer.Vector_101_vroot" use:
soup = BeautifulSoup(<your_html_as_a_string>)
soup.find_all(id="OpenLayers.Layer.Vector_101_vroot")