0

I'm trying to get Selenium to click on one of the most visited web links from the default Chrome web page.

The problem is that Selenium cannot find the element on the page and I think it has to do with the fact that a web page technically didn't get loaded. When you open up Chrome it has HTML elements there but the address bar is completely empty. I think possibly this is why Selenium can't find the link? The code is simple and finding the XPATH wasn't an issue. I just don't know if this is a function that Selenium will be able to do or not. I'm trying to do the click because the navigate() function will not work when I put in the proxy information due to the fact that Selenium doesn't have a built-in way to handle a proxy with username and password.

At the end of the day I'm trying to get the username/password box to pop up by clicking on the link. When I open the browser with Selenium programmatically and then manually click on the link the username/password box pops up. But I can't get Selenium to find the element to click on programmatically.

var did = driver.FindElement(By.XPath("//*[@id='mv-tiles']/a[1]"));
did.Click();

m

UPDATE 1: I was able to find the element when taking into consideration the iframe but clicking still is an issue.

var frm = driver.SwitchTo().Frame("mv-single");
var did = frm.FindElement(By.XPath("//*[@id='mv-tiles']/a[1]"));
//did.Click(); <-- I can see it go to the element but nothing occurs
IJavaScriptExecutor js2 = (IJavaScriptExecutor) driver;
js2.ExecuteScript("arguments[0].click();", did); 

The JavaScriptExecuter is able to click the element but Chrome blocks the redirect with the following message:

[21040:24704:1204/150143.743:ERROR:CONSOLE(1)] "Unsafe JavaScript attempt to initiate navigation for frame with URL 'chrome-search://local-ntp/local-ntp.html' from frame with URL 'chrome-search://most-visited/single.html?title=Most%20visited&removeTooltip=Don%27t%20show%20on%20this%20page&enableCustomLinks=1&addLink=Add%20shortcut&addLinkTooltip=Add%20shortcut&editLinkTooltip=Edit%20shortcut'. The frame attempting navigation is targeting its top-level window, but is neither same-origin with its target nor has it received a user gesture. See https://www.chromestatus.com/features/5851021045661696. ", source: (1)

FINAL UPDATE: I gave up and decided to do the browser extension solution for proxies with passwords: https://stackoverflow.com/a/35293222/5415162

Roro
  • 311
  • 2
  • 20
  • For me, in chrome, the home page has an iframe. You may need to select that iframe first: https://www.guru99.com/handling-iframes-selenium.html – DMart Dec 04 '19 at 19:40
  • This is probably a security issue. Otherwise a site could load that page into it's own iframe and grab the info. That is a local page and should not be accessible via outside javascript. You could try with "--no-sandbox" arg. – pcalkins Dec 04 '19 at 21:16
  • Thanks but I already have the no-sandbox argument enabled. – Roro Dec 04 '19 at 21:19

1 Answers1

0

That list of "Most Recent Pages" is actually in an iframe, which is probably why Selenium can't find it. Try updating the selector to account for the iframe, or maybe add a wait clause to allow the iframe to finish loading.

Regardless of that solution, I don't think it will act any differently than just navigating to the target URL. So to fix your root problem have you tried setting the proxy details when creating the ChromeOptions?

Jim Buck
  • 2,383
  • 23
  • 42