Selenium C# - how to use element by "nobr" - c#

I have this hml code:
<nobr class="ms-crm-Form-Title-Data autoellipsis">
Text - Some Text
I would like to get the text value by selenium driver. how can I do that? I have tried with CssSelector:
[FindsBy(How = How.CssSelector, Using = "nobr[class = ms-crm-Form-Title-Data autoellipsis]")]
public IWebElement ApplicationNumberLabel { get; set; }
but I'm getting Could not find element Error.
Thanks for your help.

If you want to find an Element with selenium in C# you do it like this:
IWebElement element = driver.FindElement(By.Id("TheId"));
In this case By.Id is the selector this can be XPath, Css, Tag, Id and many other selectors. If you want to select based on multiple css classes you need XPath.
IWebDriver driver = new FirefoxDriver();
private void button1_Click(object sender, EventArgs e)
{
driver.Navigate().GoToUrl("file:///C:/Users/notmyname/Desktop/test.html");
IWebElement element = driver.FindElement(By.XPath("//nobr[#class='ms-crm-Form-Title-Data autoellipsis']"));
textBox1.Text = element.Text;
}

Related

Cant get Innertexts from webpage using html agility pack xpath in c#

So this is my code guys.
Im trying to get the text inside a span and storage it locally. Im using html agility pack and trying to retrieve the text using xpath but the nodes dont retrieve anything and appear as null.
This is the page im trying to get the text from: https://siat.sat.gob.mx/app/qr/faces/pages/mobile/validadorqr.jsf?D1=10&D2=1&D3=15030267855_SDS150309FC7
Specifically the "Denominación o razón social" text.
namespace ObtencionDatosSatBeta
{
public partial class Form1 : Form
{
DataTable table;
public Form1()
{
InitializeComponent();
}
private void InitTable()
{
table = new DataTable("tabladedatosTable");
table.Columns.Add("Variable", typeof(string));
table.Columns.Add("Contenido", typeof(string));
//table.Rows.Add("Super Mario 64", "84%");
tabladedatos.DataSource = table;
}
private async void Form1_Load(object sender, EventArgs e)
{
InitTable();
HtmlWeb web = new HtmlWeb();
var doc = await Task.Factory.StartNew(() => web.Load("https://siat.sat.gob.mx/app/qr/faces/pages/mobile/validadorqr.jsf?D1=10&D2=1&D3=15030267855_SDS150309FC7"));
var nodes = doc.DocumentNode.SelectNodes("//*[#id=\"ubicacionForm: j_idt12:0:j_idt13: j_idt17_data\"]//tr//td//span");
var innerTexts = nodes.Select(node => node.InnerText);
}
private void tabladedatos_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
}
Any idea?
var nodes = doc.DocumentNode.SelectNodes("//*[#id=\"ubicacionForm: j_idt12:0:j_idt13: j_idt17_data\"]//tr//td//span");
The line of code above is the one that appears as null.
Use this Xpath which gets the first span under the element with the following ID: ubicacionForm:j_idt10:0:j_idt11:j_idt14_data
(//*[#id='ubicacionForm:j_idt10:0:j_idt11:j_idt14_data']//span)[1]
You can select the element using multiple different ways by copying the HTML in chrome (Ctrl + Option + J)
And then paste the HTML in Xpather where you can play around with your Xpath. Xpather.com

C# Selenium Driver - get all elements by text

private IWebDriver webDriver;
[SetUp]
public void Setup()
{
webDriver = new FirefoxDriver();
webDriver.Navigate().GoToUrl("https://localhost:44311/");
}
[Test]
public void ButtonsCount_IsCorrect_IsTrue()
{
var buttons = webDriver.FindElement(...));
...
}
Simple scenario - website contains dynamically created buttons. I want to ensure count of buttons is the same as expected.
Catching it by XPath would be easy, except XPath leads to certain element, here we want to catch multiple dynamically created elements, but each one looks like this:
<a><i></i>Details</a>
The only difference is where href leads, and I have skipped classes because they are not important here.
Conclusions are, it wouls be wise to search for all <a> which contain string Details. Now question is, how to do it in C# driver.
You can use findElements with xpath to find List of elements:
List<WebElement> elementName = driver.FindElements(By.XPath("//a[contains(text(),'Details')]"));

How to select only first available checkbox selenium

i am new with Selenium and i try to select and click on the first checkbox from the datatable
i could get the id of the checkbox with the following code
public void AssignToCompany()
{
var checkedRow = Driver.FindElement(By.XPath("//input[#type='checkbox'][1]"));
checkedRow.Click();
}
but when i run the function i get the below error and the checkbox will not be cklicked!
Update: element not interactable means that your element is hidden and can't be clicked, you should click another xpath
Try this xpath
Driver.FindElement(By.XPath(".//span[contains(#class,'ui-chkbox-icon')][1]"))
You can click on the first checkbox by using the code(Have used brackets to limit the xpath to the first index):
public void AssignToCompany()
{
var checkedRow = Driver.FindElement(By.XPath("(//input[#type='checkbox'])[1]"));
checkedRow.Click();
}
Use FindElements.
public void AssignToCompany()
{
var checkedRow = Driver.FindElements(By.XPath("(//input[#type='checkbox'])"))[0];
checkedRow.Click();
}

Select a random XPath element

Hy,
on a website there are some button:
Previously I copied the old code to the question, this is the actual code:
IWebElement btn_tag_1 = driver.FindElement(By.XPath("//*[#data-testid ='tag_eject']"));
IWebElement btn_tag_2 = driver.FindElement(By.XPath("//*[#data-testid ='tag_close']"));
IWebElement btn_tag_3 = driver.FindElement(By.XPath("//*[#data-testid ='tag_open']"));
IWebElement btn_tag_4 = driver.FindElement(By.XPath("//*[#data-testid ='tag_selected']"));
IWebElement btn_tag_5 = driver.FindElement(By.XPath("//*[#data-testid ='tag_disabled']"));
IWebElement btn_tag_6 = driver.FindElement(By.XPath("//*[#data-testid ='tag_free']"));
In a normal case when I would like to click a selected element for example "btn_tag_1" the code is:
btn_tag_1.Click();
But now I wanted to click one of them selected randomly and I'm totally stucked at this point.
Can You help me in this case?
Thank You!
If you are just looking for some help on how to randomly click one of the buttons then I would do it this way:
using System;
using OpenQA.Selenium;
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_1']"),
By.XPath("//*[#data-testid='tag_2']"),
By.XPath("//*[#data-testid='tag_3']"),
By.XPath("//*[#data-testid='tag_4']"),
By.XPath("//*[#data-testid='tag_5']"),
By.XPath("//*[#data-testid='tag_6']")
};
int index = new Random().Next(buttonsBy.Length - 1);
IWebElement button = driver.FindElement(buttonsBy[index]);
button.Click();
Create an array with your By selectors, choose a random number within the length of that array, find the element using that random number as the index and then click.
Added after comment with code:
All you need to do is change the string inside each locator to use the code in your comment.
By[] buttonsBy = {
By.XPath("//*[#data-testid='tag_eject']"),
By.XPath("//*[#data-testid='tag_close']"),
By.XPath("//*[#data-testid='tag_open']"),
By.XPath("//*[#data-testid='tag_selected']"),
By.XPath("//*[#data-testid='tag_disabled']"),
By.XPath("//*[#data-testid='tag_free']")
};

How can get a href in Selenium Test using c#

I want to select My jobs tag and then on that but there's no Id or Name for given tag just a href but when i am trying to find my cssSelector or by cssClass then it's shows erorr.So any one who can help me
enter image description here
For clicking on My Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Jobs"));
element.Click();
For clicking on New Jobs , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("New Jobs"));
element.Click();
For clicking on My Units , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("My Units"));
element.Click();
For clicking on Timesheets , you can use this code:
IWebElement element = driver.FindElement(By.LinkText("Timesheets"));
element.Click();
Hope this will work for you!
Please let me know if you have any concerns related to this.

Categories

Resources