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.
Related
I'm trying to compile a python script and record any syntax errors
I have a script called test.py that looks like
#!/usr/bin/python
import py_compile
try:
py_compile.compile("answer.py")
except py_compile.PyCompileError as e:
print(e)
answer.py is just a self contained script that doesn't do anything except print something(for now)
i also have a php exec command in a php file that runs test.py, problem is php exec doesn't get returned anything by
print(e)
I'm guessing because e here in PyCompileError isn't a string or something weird with the format, the result simply comes back empty "". When I played around with it it seems to be an object, not sure how to convert that to a string.
I tried putting it into a string but it's not working, if I change e into a string like print("You have errors") I get the correct output, so it's not my php but I want the syntax errors on the lines. Not even sure if I should be using compile() for this
I figured it out, basically I needed doraise=True and then get print(e.exc_value)
I did initially have doraise=True before people think I was lazy and not reading the documentation, but I didn't realize exc_value was the variable that had the message(I was trying to get e.msg) so I turned it off since it said by default it prints to stderr with it off, and I tried to work from there.
Please ignore
I'm currently using this code:
$blog= file_get_contents("http://powback.tumblr.com/post/" . $post);
echo $blog;
And it works. But tumblr has added a script that activates each time you enter a password-field. So my question is:
Can i remove certain parts with file_get_contents? Or just remove everything above the <html> tag? could i possibly kill a whole div so it wont load at all? And if so; how?
edit:
I managed to do it the simple way. By skipping 766 characters. The script now work as intended!
$blog= file_get_contents("powback.tumblr.com/post/"; . $post, NULL, NULL, 766);
After file_get_contents returns, you have in your hands a string. You can do anything you want to it, including cutting out parts of it.
There are two ways to actually do the cutting:
Using string functions like str_replace, preg_replace and others; the exact recipe depends on what you need to do. This approach is kind of frowned upon because you are working at the wrong level of abstraction, but in some cases it has an unmatched performance to time spent ratio.
Parsing the HTML into a DOM tree, modifying it appropriately (this time working at the appropriate level of abstraction) and then turn it back into a string and echo it. This can be more convenient to work with if your requirements are not dead simple and is easier to maintain, but it typically requires more code to be written.
If you want to do something that's most naturally expressed in HTML document terms ("cutting out this <div>") then don't be tempted and go with the second approach.
At that point, $blog is just a string, so you can use normal PHP functions to alter it. Look into these 2:
http://php.net/manual/en/function.str-replace.php
http://us2.php.net/manual/en/function.preg-replace.php
You can parse your output using simple html dom parser and display olythe contents thatyou really want to display
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
I want to create a PHP script that grabs the content of a website. So let's say it grabs all the source code for that website and I say which lines of code I need.
Is there a function in PHP that allows you too do this or is it impossible?
Disclaimer: I'm not going to use this for any illegal purposes at all and not asking you too write any code, just tell me if its possible and if you can how I'd go about doing it. Also I'm just asking in general, not for any specific reason. Thanks! :)
file('http://the.url.com') returns an array of lines from a url.
so for the 24th line do this:
$lines = file('http://www.whatever.com');
echo $lines[23];
This sounds like a horrible idea, but here we go:
Use file_get_contents() to get the file. You cannot get the source if the web server first processes it, so you may need to use an extension like .txt. Unless you password protect the file, obviously anybody can get it.
Use explode() with the \n delimiter to split the source code into lines.
Use array_slice() to get the lines you need.
eval() the code.
Note: if you just want the HTML output, then ignore the bit about the source in step 1 and obviously you can skip the whole eval() thing.
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;