using an expression like (//div[#class='nav']//a)[5] to retrieve a specific element with Selenium (triggered through phpunit) never suceeds for some reason.
The Xpath is valid, using other Xpath expressions works fine, but once the Xpath contains brakets the Selenium server (2.0rc2) starts returning ERROR: Element (//div[#class='nav']//a)[5] not found. even it that element is present.
Is this a limitation of the PHP-Webdriver for Selenium, is there some kind of workaround (to get the nth element within a node-set)?
Cheers
From topic Can't get nth node in Selenium i see you can try prepending xpath= to your expression to get it work.
This was the final solution:
xpath=(//div[#class='nav']//a)[position()=5]
Not sure why [5] didn't work, might still be an issue within phpunit
Cheers
Related
From this URL:
https://www.walsall.gov.uk/Waste/bincollections/Details/100071042591
I want to extract the indivual bin dates from the webpage.
When I inpsect with Chrome, and copy xpath for the first bin date, I get the following XPath:
//*[#id="main"]/table[1]/tbody/tr[2]/td[2]
Which looks good, but I get a length of zero from my $xpath->query object.
However, when I put my query as:
//*[#id="main"]/h3[2]
I get the right value (Green Bin, Recycling).
Can anyone tell me why the first XPath query isnt working? It looks perfectly logical to me.
The problem was with <tbody> tag, I took that out and ended with this:
//*[#id="main"]/table[1]/tr[2]/td[2]
My guess is when I inspected Chrome inserted the tbody tag when you inspect, but it is not actually present in the raw HTML served by the server.
I created a test using PHP WebDriver and Selenium. Now I want to make sure that a certain text is contained in an element. How do I do that? I tried:
$web_driver->wait(3)->until(
WebDriverExpectedCondition::textToBePresentInElement(WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €')
);
But this always ends in a TimeoutException. Isnt there a way to really use something like this:
assertTrue(WebDriverExpectedCondition::textToBePresentInElement(WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €'))
Thanks for your help!
$web_driver->wait(3)->until(
WebDriverExpectedCondition::textToBePresentInElement(
WebDriverBy::cssSelector('.cart-price span.price'), '55,00 €'
)
);
is already the assertTrue to me. It throws the TimeoutException if it cannot find the element containing certain text within 3 seconds.
If you are seeing the text on the browser, it might happen that there is no element found by the WebDriverBy or there are more than one element and the driver is getting another element which match the WebDriverBy.
If you are using PHP, you might have to change the
max_execution_time
in your PHP.ini file.
This sets the number of seconds that a PHP script can run for.
I'm using Mink and the Selenium2 Driver with Behat to run some acceptance tests and for the most part, everything is going well.
However, I'm trying to target an element based on a data-* attribute with XPath, and the test chokes on it.
I've used XPathHelper and FirePath and my XPath checks out in both of those extensions:
//html//#data-search-id='images'
That appears to target the correct element.
However, when I add the following to FeatureContext.php
$el = $this->getSession()->getPage()->find('xpath', $this->getSession()->getSelectorsHandler()->selectorToXpath('xpath', "//#data-search-id='images'"));
$el->click();
I get the following error from Behat:
invalid selector: Unable to locate an element with the xpath expression
//html//#data-search-id='images' because of the following error:
TypeError: Failed to execute 'evaluate' on 'Document':
The result is not a node set, and therefore cannot be converted
to the desired type.
Your XPath expression is a totally valid expression – it will find all #data-search-id attributes and return true if one of them is 'images', otherwise false.
But you want to click an item, and obviously clicking a boolean value is rather difficult. Query for the item fulfilling the condition instead (thus, move the comparison into a predicate):
//html//*[#data-search-id='images']
Additionally, I'd remove the //html. The HTML node must be the root node anyway, so /html would have been fine (no reason for searching it in all subtree). As you're searching for an arbitrary descendent of it, and this will not be the root node (as <html/> is), omitting it completely does not change the meaning of the XPath expression.
//*[#data-search-id='images']
I think the XPath you're looking for is:
//html//*[#data-search-id='images']
g day dear community - hello all!
well I am trying to select either a class or an id using PHP Simple HTML DOM Parser with absolutely no luck. Perhaps i have to study the manpages again and again.
Well - the DOM-technique somewhat goes over my head:
But my example is very simple and seems to comply to the examples given in the manual (simplehtmldom.sourceforge AT net/manual.htm) but it just wont work, it's driving me up the wall. Other example scripts given with simple dom work fine.
See the example: http://www.aktive-buergerschaft.de/buergerstiftungsfinder
This is the easiest example i have found ... The question is - how to parse it?
Should i do it with Perl - The example HTML page is invalid HTML.
I do not know if the Simple HTML DOM Parser is able to handle badly malformed HTML
(probably not). And then i am lost.
Well: it is pretty hard to believe - but you can get the content with file_get_contents: But you afterwards have to do the parser job! And there i have some missing parts!
Finally: if i cannot get it to run i can try out some Perl parsers eg HTML::TreeBuilder::XPath
1: check whether file_get_contents is working!!!!
2: If no use curl or fopen or telnet to read the data.
Simple Html Dom filters all the noise can process malformed tags also...
Problem might be with your data retrieving
I'm trying to fetch data from a div (based on his id), using PHP's PCRE. The goal is to fetch div's contents based on his id, and using recursivity / depth to get everything inside it. The main problem here is to get other divs inside the "main div", because regex would stop once it gets the next </div> it finds after the initial <div id="test">.
I've tryed so many different approaches to the subject, and none of it worked. The best solution, in my oppinion, is to use the R parameter (Recursion), but never got it to work properly.
Any Ideais?
Thanks in advance :D
You'd be much better off using some form of DOM parser - regex really isn't suited to this problem. If all you want is basic HTML dom parsing, something like simplehtmldom would be right up your alley. It's trivial to install (just include a single PHP file) and trivial to use (2-3 lines will do what you need).
include('simple-html-dom.php');
$dom = str_get_html($bunchofhtmlcode);
$testdiv = $dom->find('div#test',0); // 0 for the first occurrence
$testdiv_contents = $testdiv->innertext;