Facebook Webdriver dosen't open a url inside Foreach [duplicate] - php

This question already has answers here:
StaleElementReference Exception in PageFactory
(3 answers)
Closed 3 years ago.
Good afternoon,
I am trying to open several links within a foreach using Facebook WebDriver, but it only opens the first one.
The links are arriving correctly, string, but on the second link it hangs and displays the error below.
It seems to me that selenium drive has a cookie or cache problem.
foreach($subjectsList as $subject)
{
// Get all info subjects each course.
$subjectTitle = $subject->getText();
$subjectLink = trim($subject->getAttribute('href'));
$rawPage = $this->seleniumDriver->get($subjectLink);
} // end Foreach for subjectsLis
Thanks in advance guys, any help is very welcome.

It's not a cookie or a cache problem, it's conceptual.
$rawPage = $this->seleniumDriver->get($subjectLink); performs browser navigation. So, your code will correctly parse out the first link, and navigate to it. But, when it tries the second link, selenium correctly identifies that $subject is essentially a dangling pointer (well, it contains a C pointer which is technically the dangling pointer, but...) to a DOM element in the previous (deallocated) webpage/dom-tree.
To do what you're looking for, first parse out the titles/hrefs to strings, then iterate over the strings.
I'd take a look at the relevant documentation

Related

How would I take a string, find a certain character, add a character, and continue until end of string? [duplicate]

This question already has answers here:
PHP: Variables in a Postgresql Query
(2 answers)
Closed 3 years ago.
I’m working on a project where I need to use postgresql to update info. I need to take
Martin’s chik ‘n’ chips
And make change it to
Martin\’s chik \’n\’ chips
How would I do this? I’ve looked at other posts, and found out to use substr() to create the new string and strpos() to find the ‘s, and even setting a new variable to keep the position of the previous ‘
Edit: thanks everyone, clearly didn’t do enough research!
If in PHP:
Check out str_replace(). e.g.
$text = "Martin’s chik ‘n’ chips";
$apostrophe = array("'","`","‘","’");
$newtext = str_replace($apostrophe,"\'",$text);
In this specific example, if you don't have any of the 'fancy' apostrophes, check out addslashes() as this will solve everything for you

PHP regex on mention (#name) [duplicate]

This question already has answers here:
Finding #mentions in string
(6 answers)
Closed 5 years ago.
I have busting my balls on this one, and I have been trying numerous regex'es but just can't seem to get it right. (I am not that experienced in regex).
The following situation is going on, lets take this basic sentence for example;
I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?
The trick here is to get only the #john-doe and #foo-bar parts from the string, preferably in an array:
$arr = [
'#john-doe',
'#foo-bar'
];
Could someone help me get on the right track?
You can use this regex (\#(?P<name>[a-zA-Z\-\_]+)) :
<?php
$matches = [];
$text = "I recently saw #john-doe riding a bike, did you noticed that too #foo-bar?";
preg_match_all ("(\#(?P<names>[a-zA-Z\-\_]+))" ,$text, $matches);
var_dump($matches['names']);
In this example, I used the ?P<names> to name the capture groups, it's easier to get it.
I've made a Regex101 for you, and a PHP sandbox for test
https://regex101.com/r/ZFWvCG/1
http://sandbox.onlinephpfunctions.com/code/1d04ce64a2a290994bf0effd7cf8f0039f20277b

Parsing JSON output from API [duplicate]

This question already has answers here:
Parsing JSON with PHP
(6 answers)
Closed 7 years ago.
I know this one has been asked loads of times, so mods please do not close this off as no past answers have worked and I have searched already! So I'm using the HaveIBeenPwned API which outputs what is apparently JSON like so:
[
{
"Title":"000webhost"
,"Name":"000webhost"
,"Domain":"000webhost.com"
,"BreachDate":"2015-03-01"
,"AddedDate":"2015-10-26T23:35:45Z"
,"PwnCount":12345678
,"Description":"In approximately March 2015, the free web hosting provider 000webhost suffered a major data breach that exposed over 13 million customer records. The data was sold and traded before 000webhost was alerted in October. The breach included names, email addresses and plain text passwords."
,"DataClasses":[
"Email addresses"
,"IP addresses"
,"Names"
,"Passwords"
]
,"IsVerified":true
,"IsSensitive":false
,"LogoType":"png"
}
]
That is the output if I use the following:
echo $output;
json_decode($output) doesn't work. Using a foreach($output as $key) doesn't work, it says invalid argument if I use that. I have tried a normal for() loop and again no joy. What is wrong with this output? How do I get at the values within it? As you should be able to tell I'm using PHP.
Use json_decode($output, true);
Second parameter defines result data format - object (false, default) or associated array (true).

Extract site link from google URL [duplicate]

This question already has answers here:
How can I get parameters from a URL string?
(12 answers)
Closed 8 years ago.
I want to extract site link from Google URL, I need an efficient way to do this,
I have extracted this, but i am not comfortable with that like,
$googleURL = "http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/about%3Fgl%3DCA%26hl%3Den-CA&ved=0CHAQlQU&sa=X&ei=HzrCVNX-JqSzigb-94D4CQ&s=ANYYN7nQx_FiR1PuowDmXBi1oyfkI2MImg";
I want this
https://plus.google.com/110334461338830338847/
I have done this in a following way.
$first = current(explode("about", $googleURL)); // returns http://www.google.ca/local_url?dq=food+Toronto,+ON&q=https://plus.google.com/110334461338830338847/
and then,
$myLink = explode("&q=", $first);
echo $myLink[1]; // return my need => https://plus.google.com/110334461338830338847/
but there may be two "about" or "&q=" in a googleURL which can cause problem.
I know that, this googleURL will be redirected to my need, but I need that specific link for a purpose.
I guess that it is not really safe to parse that since google can change its implementation anytime.
However, if you want to get a parameter from a String url, this question covers it pretty well :
How to get parameters from a URL string?
$parts = parse_url($googleUrl);
parse_str($parts['query'], $query);
echo $query['q'];

Use SimpleXML to grab nested code [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
php simplexml issue in reading an attribute that has a ‘column - : ’ in its name
I'm in the process of learning PHP, so I'm making a weather app to display an image based on the weather. I'm using the Yahoo Weather API.
My problem is that I'm looking for a way to grab the code of the weather, located next to yweather:condition . I found simpleXML but can't figure out how to grab something that's located in something like the way Yahoo does its xml in the form of
<yweather:condition text="Fair" code="34" temp="37"
So my question is how can I grab the code="34" portion of the Yahoo weather and display it as a variable like $weathercode = 34;?
Thanks for any and all help, and I'm here to give more details if you need them!
Also, I'm not sure if the title of this post is correct, so sorry if it's not!
You can use the SimpleXMLElement::attributes call in a similar way to below.
$string = {Yahoo Weather Feed}
$xml = simplexml_load_string($string);
$weathercode = $xml->channel->item->children('yweather', true)->condition[0]->attributes()->code;
You may need to tinker to get it right, but this is just a guide.
Reference: http://php.net/manual/en/simplexmlelement.attributes.php
You could try the following:
$xml = simplexml_load_file('http://weather.yahooapis.com/forecastrss?w=12793465&u=f');
var_dump($xml);
This will load the URL and dump the SimpleXML object.
More on simplexml_load_file here

Categories