I am trying to get the URL of the second page of a yellowpages result with the following code:
var driverService = PhantomJSDriverService.CreateDefaultService();
var driver = new PhantomJSDriver(driverService);
driver.Navigate().GoToUrl(new Uri("http://www.yellowpages.com/los-angeles-ca/pizza?g=Los+Angeles%2C+CA"));
string url = driver.Url;
var next = driver.FindElementByCssSelector(".next");
next.Click();
string newUrl = driver.Url;
The "next" link is found and clicked but I do not get the new URL after calling next.Click().
Other pages work fine. I am only having problems on yellowpages right now.
Any ideas?
Try this for clicking on the web element instead of using click():
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", next);
Make sure you have turned on console output, so you could see the exact error:
service.HideCommandPromptWindow = true;
I had similar problem, and when i turned on console output I noticed the following error: "can't find variable: __doPostBack".
In my case, that was because of site declined defaut Phantom's user agent, so I had to change it (based on this answer).
Related
I have a URL with multiple paths. For example:
https://stackoverflow.com/questions/120250
I want to grab the URLS entire path and save it, then I want to call the URL later to duplicate the URL with all the paths. URL path is dynamic.
So for I have attempted this
public Shared DuplicateTheChromeBrowser()
{
String selectLinkOpeninNewTab = (Keys.Alt + "d" + Keys.Enter);
var SavedURL = Driver.Instance.Url();
SavedURL.SendKeys(selectLinkOpeninNewTab);
return this;
}
Try this out. It saves the url of the page you go to or are on, then uses JS to create a new tab and send the URL from the original page to the new tab. Initbrowser() is my method for initiating an instance, so it its not necessarily part of the solution.
public Shared DuplicateTheChromeBrowser()
{
//Initiate Browser Instance(this is my method, initiate how you did in our code which isnt visible in your question)
IWebDriver driver = InitBrowser("chrome");
//Go to page
driver.Url = "https://stackoverflow.com/questions/120250?params=testparam";
//Save Current URL
var SavedUrl = driver.Url;
//Uses JS to open new tab
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
//Changes Url of new tab to saved URL including param
driver.Url = SavedUrl;
}
I'm trying to determine if there's specific text on the page. I'm doing this:
public static void WaitForPageToLoad(this IWebDriver driver, string textOnPage)
{
var pageSource = driver.PageSource.ToLower();
var timeOut = 0;
while (timeOut < 60)
{
Thread.Sleep(1000);
if (pageSource.Contains(textOnPage.ToLower()))
{
timeOut = 60;
}
}
}
The problem is that the web driver's PageSource property isn't updated after the initial load. The page I'm navigating to loads a bunch of data via JS after the page has already loaded. I don't control the site, so I'm trying to figure out a method to get the updated HTML.
You are trying to solve the wrong problem. You need to wait for the text to appear using an XPath locator:
var wait = new WebDriverWait(driver);
var xpath = $"//*[contains(., '{textOnPage}')]";
wait.Until(ExpectedConditions.ElementIsVisible(By.XPath(xpath));
Do you really need to search entire page?
I'll reference you to here: https://stackoverflow.com/a/41223770/1387701
with this code:
String Verifytext= driver.findElement(By.tagName("body")).getText().trim();
You can then check to see if the Verifytext contains the string you're checking for.
This works MUCH better if you can narrow the location of the text down to a particular webElement other than the body.
I have two instances of the ChromiumWebBrowser in my WinForms project (Visual Studio 2012). My goal is to have the second browser instance "copy" the behavior of the user input in the first browser instance. I can successfully retrieve the input from the first browser, and I managed to hook up Selenium in the project as well.
However, I'm having one issue. Whenever Selenium sends its commands, the first browser is the one that responds to them. For the life of me, I can't seem to figure out how to make the second browser respond. Whenever I completely remove the first browser, the second one starts responding correctly, but adding the first browser again will make only have the first browser use the Selenium commands. I even tried to switch out the moments the browsers are added to the form, but to no avail: whenever there are two available, the wrong one is responsive.
Relevant code:
public BrowserManager(Controller controller, string startingUrl)
{
_controller = controller;
var settings = new CefSettings { RemoteDebuggingPort = 9515 };
Cef.Initialize(settings);
// Input browser
inputBrowser = new ChromiumWebBrowser(startingUrl);
var obj = new XPathHelper(this);
inputBrowser.RegisterJsObject("bound", obj); //Standard object registration
inputBrowser.FrameLoadEnd += obj.OnFrameLoadEnd;
// Output browser
var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };
var requestContext = new RequestContext(requestContextSettings);
outputBrowser = new ChromiumWebBrowser(startingUrl);
outputBrowser.RequestContext = requestContext;
outputBrowser.AddressChanged += InitializeOutputBrowser;
outputBrowser.Enabled = false;
outputBrowser.Name = "outputBrowser";
}
The selenium part:
public class SeleniumHelper
{
public SeleniumHelper()
{
DoWorkAsync();
}
private Task DoWorkAsync()
{
Task.Run(() =>
{
string chromeDriverDir = #"ActionRecorder\bin\x64\Debug\Drivers";
var chromeDriverService = ChromeDriverService.CreateDefaultService(chromeDriverDir);
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = #"ActionRecorder\bin\x64\Debug\ActionRecorder.exe";
options.DebuggerAddress = "127.0.0.1:9515";
options.AddArguments("--enable-logging");
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeDriverService, options))
{
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("A google search test");
query.Submit();
}
});
return null;
}
}
And finally, a screenshot for some visualization:
Some help with the issue would be very much appreciated. If i missed some crucial info, feel free to ask for it. Thanks in advance!
Greetz,
Tybs
The behavior is correct. You have one debug address and you can only have one debug address for CEF. Which means when you use Selenium it is only seeing one browser.
By default Selenium will send an command to current active Tab or Window. Now in your case you have multiple Chrome view embedded, but they are technically Chrome Tab/Windows which you have placed on the same form.
So if you are in luck below code in should be able to move you to the Window you are interested in
driver.SwitchTo().Window(driver.WindowHandles.Last());
See if it works. If it doesn't then your only other workaround would be to change the order of Adding ChromiumWebBrowser and that should reverse the window it works on.
Below are some important threads that you should read from top to bottom. Very relevant to your issue/request
https://code.google.com/archive/p/chromiumembedded/issues/421
https://github.com/cefsharp/CefSharp/issues/1076
I am trying to create a Virtual user with and redirect to content editor as below.
string userId = string.Format("{0}\\{1}", "sitecore", "testadmin");
var scUser = AuthenticationManager.BuildVirtualUser(userId, true);
scUser.RuntimeSettings.IsAdministrator = true;
scUser.RuntimeSettings.AddedRoles.Add(#"sitecore\Sitecore Client Authoring");
AuthenticationManager.Login(scUser);
string url = "/sitecore/shell/sitecore/content/Applications/Content Editor.aspx?id=%7b110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9%7d&la=en&fo=%7b110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9%7d";
url = string.IsNullOrEmpty(url) ? "/" : url;
HttpContext.Current.Response.Redirect(url, false);
But it always redirects the user to sitecore/login page.
Any idea what is the issue here?
Interesting. I'm not entirely sure that approach is a supported scenario. However, the Content Editor runs off the "shell" website, possibly that is your issue.
Try putting this code around your entire code block.
using(new SiteContextSwitcher("shell")) {
}
You need to change:
AuthenticationManager.Login(scUser);
to
AuthenticationManager.LoginVirtualUser(scUser);
I am testing a website. In the body of website has a URL. After clicking that URL it should be opened in a new tab, not in same window. After clicking the URL link it is opened in new tab browser. I have tested it manually.
I want to verify that feature by using TestComplete with C#Script. How to do it?
You can simulate a click to this link and then check whether the URL of the current page object has been changed and whether a new page object with the target URL has appeared.
function Test1()
{
var browser = Sys.Browser("firefox");
var numOfTabs = browser.FindAllChildren("ObjectType", "Page").toArray().length;
var page = browser.ToUrl("http://www.w3schools.com/html/tryit.asp?filename=tryhtml_links_target");
var pageUrl = page.Url;
frame = page.Panel(0).Panel(1).Panel(0).Panel(1).Frame("iframeResult");
frame.Link(0).Click();
if (page.Url != pageUrl)
Log.Error("The page's URL has been changed!");
if (browser.FindAllChildren("ObjectType", "Page").toArray().length == numOfTabs)
Log.Error("A new tab has not been opened!");
}