Selenium Can't Find Element Returning None or [] - python-3.x

im having trouble accessing element, here is my code:
driver.get(url)
desc = driver.find_elements_by_xpath('//p[#class="somethingcss xxx"]')
and im trying to use another method like this
desc = driver.find_elements_by_class_name('somethingcss xxx')
the element i try to find like this
<div data-testid="descContainer">
<div class="abc1123">
<h2 class="xxx">The Description<span data-tid="prodTitle">The Description</span></h2>
<p data-id="paragraphxx" class="somethingcss xxx">sometext here
<br>text
<br>
<br>text
<br> and several text with
<br> tag below
</p>
</div>
<!--and another div tag below-->
i want to extract tag p inside div class="abc1123", but it doesn't return any result, only return [] when i try to get_attribute or extract it to text.
When i try extract another element using this method with another class, it works perfectly.
Does anyone know why I can't access these elements?

Try the following css selector to locate p tag.
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").text)
OR Use get_attribute("textContent")
print(driver.find_element_by_css_selector("p[data-id^='paragraph'][class^='somethingcss']").get_attribute("textContent"))

Related

Python Selenium, find certain elements under a certain element

<section id='browse-search'>
<div>
<div>
<div>
<div class='product-pod'>
<div class='product-pod>'>
</div>
</div>
</div>
</section>
<div class='product-pod>'>
<div class='product-pod>'>
I have a webpage like this structure. and I need a cleaner way to locate elements with class='product-pod'. driver.find_elements(By.XPATH,"div[#class='product-pod']") will not work, because there are a few matched elements outside the section element.
Please advise what is the most appropriate way to locate those elements.
With what you have provided, this strategy could be built:
if the extra > that you provided is a typo, then:
For first div element:
driver.find_element(By.XPATH, "(//section[#id='browse-search']//div[#class='product-pod'])[1]")
For second div element:
driver.find_element(By.XPATH, "(//section[#id='browse-search']//div[#class='product-pod'])[2]")
If the > is not a typo, then the structure changes, and the below strategies would work:
For main div element:
driver.find_element(By.XPATH, "//section[#id='browse-search']//div[#class='product-pod']")
For inner div element:
driver.find_element(By.XPATH, "//section[#id='browse-search']//div[#class='product-pod']/div")
you can try xpath like
//section[#id='browse-search']//div[contains(#class,'product-pod')]
which will collect all product-pod classes inside section having id = browse-search

How to find all the span tag inside of an element in selenium python?

<div id="textelem" class="random">
<span class="a">
TEXT 1
</span>
<span>
<span>TEXT 2 </span>
</span>
<span>TEXT 3</span>
</div>
Python: TargetElem = self.wait.until(EC.presence_of_element_located((By.ID, "textelem")))
I want to get all the text inside of span tags of TargetElem element. How can I get all the span elements inside of TargetElem element and loop through them to get a single string of collected text. Thank you.
simply use .text
TargetElem = self.wait.until(EC.presence_of_element_located((By.ID, "textelem")))
print(TargetElem.text)
I do not think that you actually need a loop, since we are passing textelem id of div and all the span tags are inside the div, so .text should work.

How do I retrieve text from a text node in Selenium

So, essentially I want to get the text from the site and print it onto console.
This is the HTML snippet:
<div class="inc-vat">
<p class="price">
<span class="smaller currency-symbol">£</span>
1,500.00
<span class="vat-text"> inc. vat</span>
</p>
</div>
Here is an image of the DOM properties:
How would I go abouts retrieving the '1,500.00'? I have tried to use self.browser.find_element_by_xpath('//*[#id="main-content"]/div/div[3]/div[1]/div[1]/text()') but that throws an error which says The result of the xpath expression is: [object Text]. It should be an element. I have also used other methods like .text but they either only print the '£' symbol, print a blank or throw the same error.
You can use below css :
p.price
sample code :-
elem = driver.find_element_by_css_selector("p.price").text.split(' ')[1]
print(elem)

How can i click the third href link?

<ul id='pairSublinksLevel1' class='arial_14 bold newBigTabs'>...<ul>
<ul id='pairSublinksLevel2' class='arial_12 newBigTabs'>
<li>...</li>
<li>...</li>
<li>
<a href='/equities/...'> last data </a> #<-- HERE
</li>
<li>...</li>
Question is how can i get click third li tag ??
In my code
xpath = "//ul[#id='pairSublinksLevel2']"
element = driver.find_element_by_xpath(xpath)
actions = element.find_element_by_css_selector('a').click()
code works partially. but i want to click third li tag.
The code keeps clicking on the second tag.
Try
driver.find_element_by_xpath("//ul[#id='pairSublinksLevel2']/li[3]/a").click()
EDIT:
Thanks #DebanjanB for suggestion:
When you get the element with xpath //ul[#id='pairSublinksLevel2'] and search for a tag in its child elements, then it will return the first match(In your case, it could be inside second li tag). So you can use indexing as given above to get the specific numbered match. Please note that such indexing starts from 1 not 0.
As per the HTML you have shared you can use either of the following solutions:
Using link_text:
driver.find_element_by_link_text("last data").click()
Using partial_link_text:
driver.find_element_by_partial_link_text("last data").click()
Using css_selector:
driver.find_element_by_css_selector("ul.newBigTabs#pairSublinksLevel2 a[href*='equities']").click()
Using xpath:
driver.find_element_by_xpath("//ul[#class='arial_12 newBigTabs' and #id='pairSublinksLevel2']//a[contains(#href,'equities') and contains(.,'last data')]").click()
Reference: Official locator strategies for the webdriver

Python 3 BeautifulSoup4 search for text in source page

I want to search for all '1' in the source code and print the location of that '1' ex: <div id="yeahboy">1</div> the '1' could be replaced by any other string. I want to see the tag around that string.
Consider this context for example * :
from bs4 import BeautifulSoup
html = """<root>
<div id="yeahboy">1</div>
<div id="yeahboy">2</div>
<div id="yeahboy">3</div>
<div>
<span class="nested">1</span>
</div>
</root>"""
soup = BeautifulSoup(html)
You can use find_all() passing parameter True to indicate that you want only element nodes (instead of the child text nodes), and parameter text="1" to indicate that the element you want must have text content equals "1" -or any other text you want to search for- :
for element1 in soup.find_all(True, text="1"):
print(element1)
Output :
<div id="yeahboy">1</div>
<span class="nested">1</span>
*) For OP: for future questions, try to give a context, just like the above context example. That will make your question more concrete and easier to answer -as people doesn't have to create context on his own, which may turn out to be not relevant to the situation that you actually have.

Resources