html - get text color using python selenium? - python

Anyone know how to get the text color for both "office" and "room"?
office_color = "#FD7C04"
room_color = "green"
HTML code:
<tr style="background-color:#999" id="tagging">
<td><b style="white-space: nowrap;"> tag_table</b></td>
<td style="text-align:center;">
<span class="big" style="font-size:24px; font-weight:700; color: #FD7C04">office</span></td>
<td style="text-align:center;">
<span class="big" style="font-size:24px; font-weight:700; color: green" id="rigan">room</span></td>
</tr>

First, get the element you want to extract your atribute from, in this case the span. You choose your prefered method for that. One of them is:
selector="body > span:nth-child(2)"
element1=driver.find_element_by_css_selector(selector)
element2=driver.find_element_by_id("#rigan")
One way to get this is opening the html on your browser, right click on the element and "copy selector" to get a css selector.
Then get the atribute from the elements:
color1=element1.get_attribute("color")
color2=element2.get_attribute("color")

Related

Find dynamic content in div not iframe (python, selenium)

I am trying to scrape dynamic content from a webpage. The page shows the 10 first elements and then I have to click a 'next' button to access the next 10 and so on. The button clicking works fine. However, afterwards, it returns only empty lists after the button is clicked.
I would like to get the content of the following element:
//*[#id="bt-collapse-442112-slider"]/div[1]/div/div/div[1]/table/tbody/tr[1]/td[3]/div/h3
I noticed that the first div has the attribute "data-currentslidecount" which is "10" in the beginning but is added 10 each time the button is clicked.
My initial code looked like this:
> for a in range(100):
> WebDriverWait(driver, mywaits).until(EC.presence_of_element_located((By.XPATH,
> '//*[#id="bt-collapse-442112-slider"]/div[1]')))
> print(driver.find_element_by_xpath('//*[#id="bt-collapse-442112-slider"]/div[1]/div/div/div[1]/table/tbody/tr[1]/td[3]/div/h3').text)
> WebDriverWait(driver, mywaits).until(EC.element_to_be_clickable((By.XPATH,
> '//*[#id="bt-collapse-442112-slider"]/div[1]/button[2]')))
> button = driver.find_element_by_xpath('//*[#id="bt-collapse-442112-slider"]/div[1]/button[2]')
> button.click()
> button_count += 1
> print(button_count) driver.close()
Trying to find a solution I found out that often, this problem occurs when content is embedded in iframes, and that one needs to switch frames to get dynamic content. However, in, my case, there seems to be no iframe.
Also, I tried using find_element_by_css_selector instead of by_xpath because I thought I could access the div like this or maybe even loop over the slide count. However, `
find_element_by_css_selector(div[data-currentslidecount="10")
produced a syntax error. I didn't find a solution online for this specific css selector and also, I am not very hopeful that this will be the solution to my problem.
Edit:
Here is the HTML:
<div class="bt-slider row slick-initialized slick-slider" data-allitemcount="4184" data-currentslidecount="10"><button type="button" class="slick-prev slick-arrow slick-disabled" aria-disabled="true" style="display: block;"><span class="bab-slick-arrow-text">Zurück</span></button>
<div class="slick-list draggable">
<div class="slick-track" style="opacity: 1; width: 2264px; left: 0px;">
<div class="bt-slide col-xs-12 bt-standard-content slick-slide slick-current slick-active" data-slick-index="0" aria-hidden="false" style="width: 1132px;" tabindex="-1">
<table class="table bt-table-data">
<thead>
<tr>
<th>
<p>Veröffentlichung</p>
</th>
<th>
<p>Sitzung</p>
</th>
<th>
<p>Beschreibung</p>
</th>
</tr>
</thead>
<tbody>
<tr>
<td data-th="04.02.2019">
<p>
04.02.2019<br>Montag</p>
</td>
<td data-th="Sitzung">
<p>19/78</p>
</td>
<td data-th="Beschreibung">
<div class="bt-documents-description">
<h3>
78. Sitzung, 19. Wahlperiode, 01.02.2019</h3>

Get td class text with selenium

So I want to take the text of td class.
The html page
<table class="table table-striped">
<tbody>
<tr>
<td class="text-center">
<img .....>
</td>
<td>text</td>
<td>text</td>
<td class="text-center">
<a ....></a>
</td>
<td class="text-center">
TEXT I WANT TO TAKE HERE
</td>
<td class="text-center">
<a ....><i class="fa fa-times"></i></a>
</td>
</tr>
</tbody>
</table>
The text I want to take is "TEXT I WANT TO TAKE HERE".
I tried using the xpath like below but it didnt work
table = browser.find_element_by_xpath(("//div[#class='table table-striped']/tbody/tr/td[5]"));
I got an error saying:
no such element: Unable to locate element: {"method":"xpath","selector":"//div[#class='table table-striped']/tbody/tr/td[5]"}
Is it because I have multiple classes in the selector and I have to use dot?
(I tried: 'table.table-striped' but it still didnt work)
Your xpath is incorrect. You have a table tag but, you are looking for a div tag. So, you just need to replace div with table.
table = browser.find_element_by_xpath(("//table[#class='table table-striped']/tbody/tr/td[5]"));
Use below xpath to get the text
browser.find_element_by_xpath("//td[#class='text-center']").text
And use the index as well to better find your row e.g.
browser.find_element_by_xpath("//td[#class='text-center'][3]").text
Use Below xpath to get the text TEXT I WANT TO TAKE HERE
//table//tr/td[contains(text(), 'TEXT I WANT TO TAKE HERE')]
Updated Answer: You can refer any of these below mentioned xpath to get your webelement.
//td[5]
OR
//table[#class='table table-striped']//td[5]
OR
//table[#class='table table-striped']/..//following-sibling::td[5]
OR
//td[#class='text-center'][3]
In your XPath expression you are looking for a div tag, but your HTML does not have that. Perhaps you are looking to the table tag:
table = browser.find_element_by_xpath(("//table[#class='table table-striped']/tbody/tr/td[5]"));

Selenium python click icon next to a certain text

I have the following structure, where I am trying to click the second trash icon which is a button next to Test1.
<tr class=“ng-scope”>
<td class=“ng-scope”></td>
<td class=“ng-scope”></td>
<td class=“ng-scope”>
<span class=“ng-binding”>Test0</span>
</td>
<td class=“ng-scope”>
<button class=“bin btn-xs”>
<i class="glyphicon glyphicon-trash"></i>
</button>
</td>
</tr>
<tr class=“ng-scope”>
<td class=“ng-scope”></td>
<td class=“ng-scope”></td>
<td class=“ng-scope”>
<span class=“ng-binding”>Test1</span>
</td>
<td class=“ng-scope”>
<button class=“bin btn-xs”>
<i class="glyphicon glyphicon-trash"></i>
</button>
</td>
</tr>
Currently how I am implementing is by doing find_element_by_xpath where xpath is //i#class="glyphicon glyphicon-trash" and do an index searching with the given results.
This however I find quit inefficient, especially the given results can be theoretically many and I have to loop through the result list.
I tried also the following lines:
myxpath = "//*[contains(text(), 'Test1')]/following-sibling::tr/button[#class='glyphicon glyphicon-trash']"
driver.find_by_xpath(myxpath)
which does not work (because the trash icon is not actually the sibling of Test1.
How can I implement this in a better way (i.e. I want to use Test1 as anchor and click the trash button next to it and not next to Test0)?
To select the button in the row having the text:
//tr[.//text()='Test1']//button
To select the button in cell 4 in the row having the text in cell 3:
//tr[td[3]//text()='Test1']/td[4]//button
To select the cell having the text and then the button in the following cell:
//td[.//text()='Test1']/following-sibling::td[1]//button"
I'm not very clear whether you want the button or the icon...
here is for the i tag
try
//i[#class='glyphicon glyphicon-trash' and ../../../td/span/text() = "Test1"]
ps note also that:
<span class="ng-binding">Test1</span>
and
<span class="ng-binding"> Test1 </span>
are different.

check if text contained in a font tag

Im using selenium in python in order to check if a text between font tags in contained and getting the tag tbody back if it is.
the html code:
<table width="100%" border="0" cellspacing="1" cellpadding="1" bgcolor="#000000" height="80">
<tbody>
<tr bgcolor="#666699">
<td height="17" bgcolor="#CCCC99">
<font size="2"><b><font face="Arial, Helvetica, sans-serif">PARTY 1</font></b></font>
</td>
</tr>
<tr>
some html code...
</tr>
</tbody>
</table>
As you can see the this is an example i need to check in the html page if the has PARTY 1 in it and if so getting the tbody element back.
I should say that there is no way to get the element using id because there are other tables written in the same way.
I'v already tried using //font[contains(.,'PARTY 1')] but as it didnt worked
what is the best way to use selenium and check if PARTY 1 is in the and get tbody elment back??
This xpath return TBODY tag which contains FONT tag with 'PARTY 1' text:
//tbody[.//font[contains(.,'PARTY 1')]]
First get the font element using el = driver.find_element_by_xpath('insert xpath here') and then use el.text to get the text. You're code should look like this:
el = driver.find_element_by_xpath('insert xpath of font tag here')
if el.text == 'PARTY 1':
return driver.find_element_by_xpath('insert xpath of tbody here')

How to verify in selenium if the image is shown on the page?

I need to verify if a particular image is shown on page with selenium in python. HTML code looks like this when the image is displayed in a table cell:
<td role="gridcell">
<div style="text-align: center">
<img class="grid-center-image" border="0" ng-show="dataItem.device_protected" src="resources/images/icon_supported.png"/>
</div>
</td>
When the image is not displayed it looks like:
<td role="gridcell">
<div style="text-align: center">
<img class="grid-center-image ng-hide" border="0" ng-show="dataItem.device_protected" src="resources/images/icon_supported.png"/>
</div>
</td>
I do not understand how to approach this? How do I verify if the image is present on the screen?
You have to get class attribute of your img tag and then have to check that whether 'ng-hide' is present in it or not. That'll decide the presence of image on screen.
Below is the sample code in python to check visibility of image on screen.
elem = driver.find_element_by_xpath('//your/xpath/to/img/tag')
if 'ng-hide' in elem.get_attribute('class'):
print 'Image is not visible on screen'
else:
print 'Image is visible on screen'
To find out if the image icon_supported.png is hidden with a single call:
if driver.find_elements_by_css_selector("img[src$='icon_supported.png'].ng-hide") :
print "image hidden"
else :
print "image displayed"

Categories