This is the HTML I am trying to get the text 'RCOVE12776' from
<span class="">SKU</span>
": "
"RCOVE12776"
the code I am using to try and get it is:
driver.find_element_by_xpath('//*[#class=""]/[text()="SKU"]').text
I feel like I'm missing something very simple here, also there may be multiple to catch so I would need to find all the text from all the classes "" that contains SKU
If you have control over the original HTML code, use an attribute id instead of a class, and then use the driver to find the element with the id.
Try this code to get required text:
driver.find_element_by_xpath('//*[span[#class="" and .="SKU"]]').text.split(':')[-1]
Related
a In a HTML page there is this line:
<td data-sort="funny" class="coin-name tw-text-right" style="min-width: 60px;">
and I can find it by using this XPATH:
//tbody/tr/td[5]
But I only interesting to put in a variable the "funny". Keep in mind that the word "funny" is changing all the time so I need to find it and push it to variable but how do I extract this changing text?
Thank you for helping ;-)
I am not sure if it will work 100% but here is one potential solution:
If you open up that tag you will find out that the first child's second child(Refer to image in solution) has a unique id attribute.
Then, you can use that unique attribute and work your way up to the parent tag with "data-sort attribute" using Child-to-Parent Traversing using Xpath. [Refer to the image it basically explains the same approach written above][1]
[1]: https://i.stack.imgur.com/9Dc2k.png
3.Once you uniquely identify the td tag you can then use getAttribute() and store its value.
Guys I need to know how to find a text with Selenium, this one for example:
Test
I can get the text with the following code:
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[text()="Test"]'))).text
But I need to get a text in the following format:
"Key" "email#gmail.com"
I need to be able to get the above text, remembering the email and password may be different depending on the case, so I would like to get the full value of the string from the .com of the string, since the email will always have a .com, so in this case , I need to be able to find the .com and after finding me return the full value of the string.
Use contains in xpath to find a text that has .com. You can also use ends-with. The xpath would be something like this:
//*[contains(text(),'.com')]
Or
//*[ends-with(text(),'.com')]
I am new to Python and BeautifulSoup. So please forgive me if I'm using the wrong terminology.
I am trying to get a specific 'text' from a div tag/element that has multiple attributes in the same .
<div class="property-item" data-id="183" data-name="Brittany Apartments" data-street_number="240" data-street_name="Brittany Drive" data-city="Ottawa" data-province="Ontario" data-postal="K1K 0R7" data-country="Canada" data-phone="613-688-2222" data-path="/apartments-for-rent/brittany-apartments-240-brittany-drive-ottawa/" data-type="High-rise-apartment" data-latitude="45.4461070" data-longitude="-75.6465360" >
Below is my code to loop through and find 'property-item'
for btnMoreDetails in citySoup.findAll(attrs= {"class":"property-item"}):
My question is, if I specifically want the 'data-name' and 'data-path' for example, how do I go about getting it?
I've searched google and even this website. Some were saying using the .contents[2]. But I still wasn't able to get any of it.
Once you have extracted the element (which findAll does one at a time) you can access attributes as though they were dictionary keys. So for example the following code:
data = """<div class="property-item" data-id="183" data-name="Brittany Apartments" data-street_number="240" data-street_name="Brittany Drive" data-city="Ottawa" data-province="Ontario" data-postal="K1K 0R7" data-country="Canada" data-phone="613-688-2222" data-path="/apartments-for-rent/brittany-apartments-240-brittany-drive-ottawa/" data-type="High-rise-apartment" data-latitude="45.4461070" data-longitude="-75.6465360" >"""
import bs4
soup = bs4.BeautifulSoup(data)
for btnMoreDetails in soup.findAll(attrs= {"class":"property-item"}):
print btnMoreDetails["data-name"]
prints out
Brittany Apartments
If you want to get the data-name and data-path attributes, you can simply use the dictionary-like access to Tag's attributes:
for btnMoreDetails in citySoup.findAll(attrs={"class":"property-item"}):
print(btnMoreDetails["data-name"])
print(btnMoreDetails["data-path"])
Note that you can also use the CSS selector to match the property items:
for property_item in citySoup.select(".property-item"):
print(property_item["data-name"])
print(property_item["data-path"])
FYI, if you want to see all the attributes use .attrs property:
for property_item in citySoup.select(".property-item"):
print(property_item.attrs)
I want to know how I can collect line, mailto link using selenium python the emails contains # sign in the contact page I tried the following code but it is somewhere works and somewhere not..
//*[contains(text(),"#")]
the emails formats are different somewhere it is <p>Email: name#domain.com</p> or <span>Email: name#domain.com</span> or name#domain.com
is there anyway to collect them with one statement..
Thanks
Here is the XPath you are looking for my friend.
//*[contains(text(),"#")]|//*[contains(#href,"#")]
You could create a collection of the link text values that contain # on the page and then iterate through to format. You are going to have to format the span like that has Email: name#domain.com anyway.
Use find_elements_by_partial_link_text to make the collection.
I think you need 2 XPath. First XPath for finding element that contains text "Email:", second XPath for element that contains attribute "mailto:".
//*[contains(text(),"Email:")]|//*[contains(#href,"mailto:")]
I am using python along with scrapy and selenium.I want to extract the text from the h1 tag which is inside a div class.
For example:
<div class = "example">
<h1>
This is an example
</h1>
</div>
This is my tried code:
for single_event in range(1,length_of_alllinks):
source_link.append(alllinks[single_event])
driver.get(alllinks[single_event])
s = Selector(response)
temp = s.xpath('//div[#class="example"]//#h1').extract()
print temp
title.append(temp)
print title
Each and every time I tried different methods I got an empty list.
Now, I want to extract "This is an example" i.e h1 text and store it or append it in a list i.e in my example title.
Like:
temp = ['This is an example']
Try the following to extract the intended text:
s.xpath('//div[#class="example"]/h1/text()').extract()
For once, it seems that in your HTML the class attribute of the is "example" but in your code you're looking for other class values; At least for XPath queries, keep in mind that you search by exact attribute value. You can use something like:
s.xpath('//div[contains(#class, "example")]')
To find an element that has the "example" class but may have additional classes. I'm not sure if this is a mistake or this is your actual code. In addition the fact that you have spaces in your HTML around the '=' sign of the class attribute may not be helping some parsers either.
Second, your query used in s.xpath seems wrong. Try something like this:
temp = s.xpath('//div[#class="example"]/h1').extract()
Its not clear from your code what s is, so I'm assuming the extract() method does what you think it does. Maybe a more clean code sample would help us help you.