<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...
Related
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()
I'm trying to use selenium with python to make some tests. I'm having trouble in selecting an element. this element makes is a part of a drop-down list and it looks like this:
<li data-original-index="16">
<a tabindex="0" class="" data-normalized-text="<span class='text'>Porto</span>">
<li data-original-index="17">
<a tabindex="0" class="" data-normalized-text="<span class='text'>Santarem</span>">
And so on. I want to select the one with the span text "Porto".
I tried the following, but with no success:
driver.find_element_by_xpath("//span[text()="Porto"]")
Any idea on how can I do this?
Try
driver.find_element_by_link_text("Porto");
Based on the HTML you linked it seems like it might be:
driver.find_element_by_xpath("//a[#data-normalized-text="<span class='text'>Porto</span>"]")
I could be of more help if you posted all the HTML.
Here is the HTML I'm dealing with
<a class="_54nc" href="#" role="menuitem">
<span>
<span class="_54nh">Other...</span>
</span>
</a>
I can't seem to get my XPath structured correctly to find this element with the link. There are other elements on the page with the same attributes as <a class="_54nc"> so I thought I would start with the child and then go up to the parent.
I've tried a number of variations, but I would think something like this:
crawler.get_element_by_xpath('//span[#class="_54nh"][contains(text(), "Other")]/../..')
None of the things I've tried seem to be working. Any ideas would be much appreciated.
Or, more cleaner is //*[.='Other...']/../.. and with . you are directly pointing to the parent element
In other scenario, if you want to find a tag then use css [role='menuitem'] which is a better option if role attribute is unique
how about trying this
crawler.get_element_by_xpath('//a[#class="_54nc"][./span/span[contains(text(), "other")]]')
Try this:
crawler.get_element_by_xpath('//a[#class='_54nc']//span[.='Other...']');
This will search for the element 'a' with class as "_54nc" and containing exact text/innerHTML "Other...". Furthermore, you can just edit the text "Other..." with other texts to find the respective element(s)
I am trying to make a program to collect links and some values from a website. It works mostly well but I have come across a page in which it does not work.
With firebug I can see that this is the html code of the illusive "link" (cant find it when viewing the pages source thou):
<a class="visit" href="/tet?id=12&mv=13&san=221">
221
</a>
and this is the script:
<td><a href=\"/tet?id=12&mv=13&san=221\" class=\"visit\">221<\/a><\/td><\/tr>
I'm wondering how to get either the "link" ("/tet?id=12&mv=13&san=221") from the html code and the string "221" from either the script or the html using selenium, mechanize or requests (or some other library)
I have made an unsuccessful attempt at getting it with mechanize using the br.links() function, which collected a number of links from the side, just not the one i am after
extra info: This might be important. to get to the page I have to click on a button with this code:
<a id="f33" class="button-flat small selected-no" onclick="qc.pA('visitform', 'f33', 'QClickEvent', '', 'f52'); if ($j('#f44').length == 0) { $j('f44').style.display='inline'; }; $j('#f38').hide();qc.recordControlModification('f38', 'DisplayStyle', 'hide'); document.getElementById('forumpanel').className = 'section-3'; return false;" href="#">
load2
</a>
after which a "new page" loads in a part of the window (but the url never changes)
I think you pasted the wrong script of yours ;)
I'm not sure what you need exactly - there are at least two different approaches.
Matching all hrefs using regex
Matching specific tags and using getAttribute(...)
For the first one, you have to get the whole html source of the page with something like webdriver.page_source and using something like the following regex (you will have to escape either the normal or the double quotes!):
<a.+?href=['"](.*?)['"].*?/?>
If you need the hrefs of all matching links, you could use something similar to webdriver.find_elements_by_css_selector('.visit') (take care to choose find_elements_... instead of find_element_...!) to obtain a list of webelements and iterate through them to get their attributes.
This could result in code like this:
hrefs = []
elements = webdriver.find_elements_by_css_selector('.visit')
for element in elements:
hrefs.append(element.getAttribute('href'))
Or a one liner using list comprehension:
hrefs = [element.getAttribute('href') for element \
in webdriver.find_elements_by_css_selector('.visit')]
With XPath, how to select all the images that are not inside <a> tag?
For example, here:
<a href='foo'> <img src='bar'/> </a>
<img src='ham' />
I should get "ham" image as a result. To get the first image, I would use \\a\\img. Is there anything like \\not(a)\\img ?
I use python + lxml so python hacks are welcome, if pure xpath would be to hairy.
That's easily done with
//img[not(ancestor::a)]
Read the spec on XPath axes if you want to find out about the other ones besides ancestor.