C
C#4mo ago
Yasaki Kondo

How to click on link?

help please, I'm trying to do automation but it can't seem to find the link
No description
4 Replies
Yasaki Kondo
Yasaki KondoOP4mo ago
C#
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;

namespace Lab6_KiemThuNangCao
{
internal class Program
{
static void Main(string[] args)
{
//Open Chrome
IWebDriver driver = new ChromeDriver();

//go to link
driver.Navigate().GoToUrl("https://lms.poly.edu.vn");

Thread.Sleep(1000);

//login
IWebElement menuButton = driver.FindElement(By.LinkText("Ha Noi"));
menuButton.Click();

// Go back to the home page and refresh the browser
driver.Navigate().Back();
driver.Navigate().Refresh();

// Enter the login credentials and click the login button
IWebElement usernameField = driver.FindElement(By.Id("thieuddph45736"));
IWebElement passwordField = driver.FindElement(By.Id("Thieu1810"));
IWebElement loginButton = driver.FindElement(By.Id("login-btn"));

//quit
driver.Quit();
}
}
}
C#
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium;

namespace Lab6_KiemThuNangCao
{
internal class Program
{
static void Main(string[] args)
{
//Open Chrome
IWebDriver driver = new ChromeDriver();

//go to link
driver.Navigate().GoToUrl("https://lms.poly.edu.vn");

Thread.Sleep(1000);

//login
IWebElement menuButton = driver.FindElement(By.LinkText("Ha Noi"));
menuButton.Click();

// Go back to the home page and refresh the browser
driver.Navigate().Back();
driver.Navigate().Refresh();

// Enter the login credentials and click the login button
IWebElement usernameField = driver.FindElement(By.Id("thieuddph45736"));
IWebElement passwordField = driver.FindElement(By.Id("Thieu1810"));
IWebElement loginButton = driver.FindElement(By.Id("login-btn"));

//quit
driver.Quit();
}
}
}
https://lms.poly.edu.vn/ here's the link on the website and the word Ha Noi is the think I'm trying to get to click
SleepWellPupper
SleepWellPupper4mo ago
It's not an anchor tag, it's a p tag. You could use a css selctor to click it: div.max-lg\:hidden:nth-child(2) > div:nth-child(1) > div:nth-child(1) > button:nth-child(1) > p:nth-child(1)
substitute
substitute4mo ago
xpath is probably best here
/html/body/div/div/div[1]/div[2]/div/div[2]/div[1]/div[1]/button/p
/html/body/div/div/div[1]/div[2]/div/div[2]/div[1]/div[1]/button/p
or by using the FindElements on p and grabbing the one with the exact text. (they could also just reimplement the LinkText locator for p, which is basically the above^)
SleepWellPupper
SleepWellPupper4mo ago
agreed, it's more robust and reads more easily
Want results from more Discord servers?
Add your server