With Selenium/python how to click buttons who match a certain condition - python

I'm currently working with Selenium and Python to create a script that helps with booking free spots on a website for internships so you don't have to check it all the time.
I found the clickable element but now I don't know how to add the condition. I would like to make a text attribute the condition, which is under a td tag below the "button" but don't know how. It shows how many places are available to the total amount of places (x/y, so I'd make it 1/ ). Is it even possible?
Maybe I could do it by the green color but I can't find where it is anchored in the HTML document
browser.get('https://www.pj-portal.de/index_hro.php?PAGE_ID=101')
delay = 2
browser.find_elements_by_xpath("//*[starts-with(text(),'1/')]")
print("found")
Liste1 = browser.find_elements_by_xpath("//*[starts-with(text(),'1/')]")
print(len(Liste1))
B = browser.find_element_by_xpath("//img[#src='images /wunsch_setzen_button.svg']")
B.click()
print("button")
Part of HTML
<img class="aktion_tertial_wunsch_setzen " data-pj_traineeship_tertial_id="166037"
data-pj_general_traineeship_id="8487" data-changetype="24"
src="images/wunsch_setzen_button.svg" alt="+" title="Wunsch">
<td class="hinweise_leer verfuegbar buchungsphase "> </td>
<td class=" tertial_verfuegbarkeit verfuegbar buchungsphase ">1/3</td>
Thanks you in advance

Did you try to use position () method in xpath ?
for example:
// xpath using position() targeting the first element starts-with(text(),'1/'
res= browser.find_elements_by_xpath("//*[starts-with(text(),'1/')][position()=1]")
res[0].click()

Related

Select another item then searched Selenium (Python)

So basically what I need help in is:
I want to search for BBXXCODE with selenium-3.141.0, and Python 3.8.5.
I'm using driver.find_element_by_xpath("//*[contains(text(),'BBXXCODE')]" which is working as expected. But I need to somehow extract the id of the <tr> tag on top, because I want to use driver.find_element_by_xpath('//*[#id="tr9"]/td[2]/h1').click() to click on the I need to select this text.
Is there a way to do this or should I stay searching via the I need to select this text instead of the code?
This is the code piece:
<tr id='tr9'>
<td></td>
<td role='gridcell'>
<span class='link'>I need to select this</span>
</td>
<td role='gridcell'>
BBXXCODE
</td>
</tr>
I hope it is mostly clear, if not, feel free to tell me I'll try to add more details to it.
Thanks in advance
Rather than get the ID and then find the other element (in two steps), I would suggest, if possible, that you just get it in one step. You can use the XPath below to find the desired SPAN based on the location of the BBXXCODE.
//tr[./td[contains(text(),'BBXXCODE')]]/td/span
To extract the id of the <tr> tag with respect to the text BBXXCODE you can use either of the following Locator Strategies:
Using xpath and parent:
tr_id = driver.find_element_by_xpath("//td[contains(., 'BBXXCODE')]//parent::tr[1]").get_attribute("id")
Using xpath and ancestor:
tr_id = driver.find_element_by_xpath("//td[contains(., 'BBXXCODE')]//ancestor::tr[1]").get_attribute("id")

How to extract the img title next to span as per the given HTML through Selenium and Python

I am making a web crawling for checking a kind of availability.
I want to check the title of the specific time. However, if the title is 'NO', there is no href, otherwise there is a href. Therefore, it's xpath depends on the title. The title name changes every time. So i can't check by xpath.
If I want to check the availability of 09:00~11:00, how can do that?
I tried to find by XPATH. However, since the XPATH changes as I told, I can't to check the specific time i want.
Thanks in advance.
Below is the HTML code.
<span class="rs">07:00~09:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">09:00~11:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">11:00~13:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
<span class="rs">13:00~15:00</span><img src="../images/reservation_btn03.gif" title="YES"><br>
<span class="rs">15:00~17:00</span><img src="../images/reservation_btn03.gif" title="YES"><br>
<span class="rs">17:00~19:00</span><img src="../images/reservation_btn03.gif" title="YES"><br>
<span class="rs">19:00~21:00</span><img src="../images/reservation_btn04.gif" title="NO"><br>
As per the HTML you have shared to check the availability of any timespan e.g. 09:00~11:00 you can use the following solution:
You can create a function() which will take an argument as the timespan and extract the availability as follows:
def check_availability(myTimeSpan):
print(driver.find_element_by_xpath("//span[#class='rs'][.='" + myTimeSpan + "']//following::img[1]").get_attribute("title"))
Now you can call the function check_availability() with any timespan as follows:
check_availability("09:00~11:00")
If the text 09:00~11:00 is fixed, you can locate the img element like this -
element = driver.find_element_by_xpath("//span[#class='rs' and contains(text(),'09:00~11:00')]/following-sibling::img")
To check whether the title attribute of the element is "YES" -
if element.get_attribute("title") == 'YES':
// do whatever you want
To get the href attribute of your required element-
source = driver.find_element_by_xpath("//span[#class='rs' and contains(text(),'09:00~11:00')]/following-sibling::img[#title='YES']/preceding-sibling::a").get_attribute("href")

How to search two driver.find_element_by_partial_link_text

I am trying to create an automated bot to purchase items from supreme python/selenium.
When I am on the products page and I use a driver.find_element_by_partial_link_text('Flight Pant') to find the product I want to buy, however I also want to select the colour of the product so I use a driver.find_element_by_partial_link_text('Black') but by doing this I am returned with the first Black product on the page instead of Flight pants that are Black. Any idea how I would achieve this goal?
here is the site link where I am try to achieve this,
http://www.supremenewyork.com/shop/all/pants
Note - I am unable to use xpaths for this, as the products change on a weekly bases so I would be unable to get the xpath for the product before it goes live on the site.
Any advice or guidance would be greatly appreciated.
You can use XPath, but the maneuver is slightly trickier. The XPath would be:
driver.find_element_by_xpath('//*[contains(text(), "Flight Pant")]/../following-sibling::p/a[contains(text(), "Black")]')
Assuming the structure of the page doesn't change on a weekly basis... To explain my XPath:
//*[contains(text(), "Flight Pant")]
Select any node that contains the text "Flight Pant". These are all <a> tags.
/../following-sibling::p
Notice how the DOM looks:
<h1>
<a class="name-link" href="/shop/pants/dfkjdafkj">Flight Pant</a>
</h1>
<p>
<a class="name-link" href="/shop/pants/pvfcp0txzy">Black</a>
</p>
So we need to go to the parent and find its sibling that is a <p> element.
/a[contains(text(), "Black")]
Now go to the <a> tag that has the text Black.
The reason there's not really any other alternative to XPath is because there's no unique way to identify the desired element by any other means (tag name, class, link text, etc.)
After finding elements by the link text "Flight pants" , iterate over each found result and extract its css color attributes. Its a psuedo-code. You have to fine tune the specific color extraction web elements.
elements = driver.find_elements_by_partial_link_text("Flight Pants")
for element in elements :
if(element.get_css_value('color').lower() == "black")
element.click()
break

click on an image with selenium in python bindings

<a onkeypress="if(event.keyCode == 13 || event.which == 13){ClientReportRpt…_ctl02.ActionHandler('Toggle','118iT0R0R0x1');}return false;" onclick="ClientReportRptMain_ctl02.ActionHandler('Toggle','118iT0R0R0x1');return false;" style="cursor:pointer;" tabindex="4">
<img border="0" alt="+" src="/SIMS/Reserved.ReportViewerWebControl.axd?ReportSession=cfsd…Type=ReportImage&ResourceStreamID=10.50.4000.0TogglePlus.gif">
</img>
</a>
This is the part of the code and there are alot of similar images on the web pages where the value '118iT0R0R0x1' and tab index = '4' is not constant for the different position of the same image in the web page.
I am using Selenium in python bindings. Any help would be appreciated. Thanks :)
The anchor has the "onclick" handler, not the image. You need to "click" on the anchor.
anchor_element = browser.find_element_by_*(...)
anchor_element.click()
Where anchor_element explains itself. To find anchor elements you do this:
http://selenium-python.readthedocs.org/locating-elements.html
I cannot tell more, because your code fragment contains only two elements. In order to find the right elements, I would have to know more about which elements are you looking for...

my xpath is not working

I am trying to grab data from something that looks like this:
<html>
<table>
<tr>
<td>
<div>
<tr>
<td>
<div>
I think this is basically what I was looking at earlier, so I did $x('//table/tr/td/div'). This returned like 10 divs, all the ones I wanted. However, I couldn't find a way to navigate through these 10 divs. Wouldn't it just be $x('//table/tr/td/div[?]'), where ? can be 1-10? BTW this is a dynamic table, each div is a different module to navigate to a part of the page, so I am trying to automate logging in and going to a certain module, yet I can only select all 10 and not a single module. In this html, div 1 and div 2 would be something like "security module" and "ticketing module".
First just a heads up, you may want to try Firebug+Firepath on Firefox, which is a more handy tool than Chrome's developer tool.
For your question, you can do this either by xpath or Selenium
Using Xpath's index (starts from one), say you want the 2nd:
# second div's xpath: (//table/tr/td/div)[2]
second_div = driver.find_element_by_xpath("(//table/tr/td/div)[2]")
Using Selenium's index (i.e get all ten divs from your xpath, then index them starting from zero):
all_divs = driver.find_elements_by_xpath("//table/tr/td/div") # all_divs should have 10 elements
second_div = all_divs[1] # zero-based index

Categories

Resources