C# - Selenium WebDriver can't get Inspect Element code - c#

(Previous thread)
(Thread for the same problem)
I want to extract information from a website, but in order to do that I need to login. Basically I can't authenticate with a HTTP/Web/Server request so I have to do it via browser, using WebDriver Selenium (Chrome). Now since the information can't be found through "view source-code", driver.PageSource doesn't work for example. Now when I do what has been suggested in the mentioned thread, I still get the wrong source code (there has also been the same problem reported in the comments). I need to get the code from the Inspector.
Any ideas? Thanks in advance!
class Program
{
static void Main(string[] args)
{
string url = "link";
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);
IWebElement email = driver.FindElement(By.Id("login-email-input-field"));
IWebElement password = driver.FindElement(By.Id("login-password-input"));
email.SendKeys("email");
password.SendKeys("password");
IWebElement login = driver.FindElement(By.Id("login-btn"));
login.Click();
driver.Navigate().GoToUrl(url);
Thread.Sleep(10000);
//Trying to get the inspect element code here
IWebElement element = driver.FindElement(By.Id("id"));
var html = element.GetAttribute("innerHTML");
Console.WriteLine(html);
}
}
}

Related

TikTok return "Too many attempts. Try again later" when login with Selenium + C#

I want to login TikTok with Selenium and C#, but when click Log In button, Tiktok return
Too many attempts. Try again later.
Is there any way for me to fix it? I'd try many ways in another topics or some websites, but the problem is still not solved.
This is full function:
public string LoginTikTok(string user, string pass)
{
new WebDriverManager.DriverManager().SetUpDriver(new ChromeConfig());
var option = new ChromeOptions();
option.AddArguments("--profile-directory=Default");
option.AddArguments("--test-type");
option.AddArguments("--disable-extensions");
option.AddArguments("--disable-infobars");
option.AddArguments("--disable-notifications");
option.AddArguments("--enable-automation");
option.AddArguments("--disable-popup-blocking");
IWebDriver driver = new ChromeDriver(options: option);
driver.Navigate().GoToUrl("https://www.tiktok.com/login/phone-or-email/email");
IWebElement userName = driver.FindElement(By.XPath(_tikTokConfigSetting.UserNameXPath));
userName.SendKeys($"{user}");
IWebElement passWord = driver.FindElement(By.XPath(_tikTokConfigSetting.PassWordXPath));
passWord.SendKeys($"{pass}");
IWebElement logIn = driver.FindElement(By.XPath(_tikTokConfigSetting.LogInButtonXPath));
logIn.Click();
return "OK";
}
Thanks for your attention :)

NoSuchElementException identifying username field within Instagram login page using Selenium and C#

I am getting this error and can't solve it.
This is all the code. I heard adding waiting time might solve the issue but not sure how to add it.
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.instagram.com");
IWebElement userName = driver.FindElement(By.Name("username"));
IWebElement password = driver.FindElement(By.Name("password"));
You could call the Until method from an instance of a WebDriverWait class, which you could tell to wait until it finds, say, element username. For example:
var wait = new WebDriverWait(driver, TimeSpan.FromMilliseconds(10000));
wait.Until(x => x.FindElement(By.Name("username")));
Hope that helps, and if you're crawling Instagram is for the right purpose :)
The identify the username field on Instagram login page you have to induce WebDriverWait for the desired ElementToBeClickable() and you can use either of the following Locator Strategies:
Using ExpectedConditions:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.instagram.com");
IWebElement userName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.Name("username")));
Using lambda expression:
IWebDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.instagram.com");
IWebElement userName = new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(x=> x.FindElement(By.Name("username"));
Your issue is not necessarily your code 100%. It really depends on how you are hitting the site. The way I access IG is to login from another site. When I lick login to IG I get the IG popup. Then I need to:
-switch to the popup
-Login
-Handle all of the other questions
-then switch to main
You may need to tweak this based on what you are doing but this should work.
public void InstagramLogin()
{
Driver.FindElement(By.XPath("//button[#class='uppy-DashboardTab-btn']//div[contains(text(), 'Instagram')]")).Click();
Driver.FindElement(By.XPath("//button[contains(text(), 'Connect to Instagram')]")).Click();
Driver.SwitchToPopup();
Driver.WaitForElementDisplayed_byXPath("//input[#name='username']"));
Driver.FindElement(By.XPath("//input[#name='username']")).SendKeys("MyEmail#gmail.com");
Driver.FindElement(By.XPath("//input[#name='password']")).SendKeys("myPassword");
Driver.FindElement(By.XPath("//button[#type='submit']")).Click();
Driver.WaitForElementDisplayed(By.XPath("//button[text()='Not Now']"));
Driver.FindElement(By.XPath("//button[text()='Not Now']").Click();
bool ele = Driver.IsElementPresent(By.XPath("//button[contains(text(), 'Continue')]"));
if (ele)
{
Driver.FindElement(By.XPath("//button[contains(text(), 'Continue')]")).Click();
Driver.WaitForElementNoLongerDisplayed_byXPath("//button[contains(text(), 'Continue')]");
}
Driver.GoToMainHandle();
}

Can the PageSource property in Selenium be updated as JavaScript loads data?

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.

How can I use click event on multiple page in the same test cases on Selenium and C#

How can I use click event on multiple page in the same test cases on Selenium and C#
For example:
Go to Google Homepage.
Search the resulting Test
Click on the Search button,
The user gets Redirected to the Searched Result page, but when I am trying to select any link it showing error.
public void opengoogle()
{
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver wd = new ChromeDriver(option);
try
{
wd.Navigate().GoToUrl("https://www.google.co.in/");
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
Thread.Sleep(2000);
wd.FindElement(By.CssSelector("#lst-ib")).Click();
wd.FindElement(By.CssSelector("#lst-ib")).Clear();
wd.FindElement(By.CssSelector("#lst-ib")).SendKeys("Test");
wd.FindElement(By.CssSelector("#lst-ib")).Submit();
}
finally { }
public void TestMethod1()
{
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver webDriver = new ChromeDriver(option);
try
{
webDriver.Navigate().GoToUrl("https://www.google.lk/");
WebDriverWait wait = new WebDriverWait(webDriver, new TimeSpan(0, 0, 0, 30));
IWebElement searchTextField= wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("lst-ib")));
searchTextField.SendKeys("Test");
IWebElement searchButton = wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".lsb")));
searchButton.Click();
wait.Until(ExpectedConditions.TitleContains("Test"));
IWebElement searchResultLink= wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a[href='http://www.speedtest.net/']")));
searchResultLink.Click();
}
finally { }
}
You can try above code.
It explains following process.
First move to Google search page.
Insert "Test" in to search text field.
Then click on Google search button.
After Navigate to search results page, click on "www.speedtest.net" link.
You can try out this code:
ChromeOptions option = new ChromeOptions();
option.AddArgument("--headless");
ChromeDriver wd = new ChromeDriver(option);
wd.Navigate().GoToUrl("https://www.google.co.in/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromMinutes(1));
wait.Until(ExpectedConditions.ElementToBeClickable(By.Name("q")));
wd.FindElement(By.Name("q")).SendKeys("Test"+Keys.RETURN);
wait.Until(ExpectedConditions.VisibilityOfElementLocated(By.CssSelector("a[href='http://www.speedtest.net/']")));
driver.FindElement(By.CssSelector("a[href='http://www.speedtest.net/']")).Click();
P.S :
1. I'm not good in C# though I've tried to convert a java code to C#. Hope this will be helpful for you.
2. This will always open the speedtest.net website. If you want something else, you can have it by replacing the cssSelector which I have written.

NoSuchElementException in c# for google search box using selenium webdriver

I'm trying to automate a simple search using selenium webdriver. My code is to launch the google.co.uk page in IE, wait until the search box has appeared, locate the search box and input "compare the market". However I keep getting a NoSuchElementException from the application, even though I can see it on the page.
My code:
class Program
{
static void Main(string[] args)
{
string searchEngine = "www.google.co.uk";
IWebDriver IEbrowser = new InternetExplorerDriver(#"C:\Drivers");
IEbrowser.Url = searchEngine;
WebDriverWait wait = new WebDriverWait(IEbrowser, TimeSpan.FromSeconds(25));
IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("q"));
});
var searchBox = IEbrowser.FindElement(By.Id("lst-ib"));
searchBox.SendKeys("compare the market");
}
}
Error Message:
An exception of type 'OpenQA.Selenium.NoSuchElementException' occurred in WebDriver.dll but was not handled in user code
Additional information: Unable to find element with name == q
HTML info of the search box:
I get the same error message if I try to use any of the other page element locators like Id. But this only appears to error out using the google search page.
You've kinda overengineered this task. :) The code below works fine.
string searchEngine = "www.google.co.uk";
IWebDriver IEbrowser = InternetExplorerDriver(#"C:\Drivers");
IEbrowser.Navigate().GoToUrl(searchEngine);
IEbrowser.FindElement(By.Id("lst-ib")).SendKeys("compare the market");
If you want to wait for an element, there is a simpler way to do this using ExpectedConditions. See an example below. Read the linked docs to see the other available conditions that can be used.
WebDriverWait wait = new WebDriverWait(IEbrowser, TimeSpan.FromSeconds(25));
wait.Until(ExpectedConditions.ElementExists(By.Name("q")));

Categories