keylogger
keylogger

Reputation: 872

Webdriver findElement by input class returns "no such element"

I want to use Webdriver to write something in a table. So, basically I have a table with X rows and Y columns. Each field is marked with e.g. row 0 col 2 in the .

The codes look like this

<!-- language: lang-html -->
<div id="ScheduleTable-01" class="widget Scheduletable suppress-errors Schedule-grid" data-widget="ScheduleTable">
<div class="grid-wrapper">
<table class="nostyles weekmode hourstype fullmonth" style="width: 100%;">
<thead>
<tbody>
<tr id="1165051795" class="Schedule-row row0 1165051795 key_AuthoriserId-1132_JobId-A00890131S_TaskId-OPT1_TsCode-01" data-row-index="0" data-job="EAST BUILDINGS">
<td class="Schedule-col details">
<td class="Schedule-col timeslots">
<div class="timeslots-container" style="opacity: 1; visibility: visible;">
<div class="timeslot d1424343600000 row0 col0 current" data-time="1424343600000" style="width: 3.22581%;">
<div class="timeslot d1424430000000 row0 col1 current" data-time="1424430000000" style="width: 3.22581%;">
<input class="row0 col1 widget class changed color black hi" type="text" autocomplete="off">
</div>
<div class="timeslot d1424516400000 row0 col2 current" data-time="1424516400000" style="width: 3.22581%;">
<input class="row0 col2 widget" type="text" autocomplete="off">
</div>
<div class="timeslot d1424602800000 row0 col3 current" data-time="1424602800000" style="width: 3.22581%;">
<div class="timeslot d1424689200000 row0 col4 current" data-time="1424689200000" style="width: 3.22581%;">
<div class="timeslot d1424775600000 row0 col5 current" data-time="1424775600000" style="width: 3.22581%;">

I want to write "1.1" in the table row 0 column 1, so this HTML part is what I am after

<!-- language: lang-html -->
<div class="timeslot d1424516400000 row0 col2 current" data-time="1424516400000" style="width: 3.22581%;">
<input class="row0 col2 widget" type="text" autocomplete="off">
</div>

My Selenium code looks like this

<!-- language: lang-java -->
WebElement writeSomething = driver.findElement(By.className("row0.col1.widget"));
            writeSomething.sendKeys("1.1");

Selenium says org.openqa.selenium.NoSuchElementException: no such element

But if I use xpath, it works fine

<!-- language: lang-java -->
WebElement writeSomething = driver.findElement(By.xpath(".//*[@id='1165051795']/td[2]/div/div[2]/input"));
writeSomething.sendKeys("1.1");

How can I do findElement by className correctly? Thanks.

Upvotes: 1

Views: 371

Answers (1)

alecxe
alecxe

Reputation: 473863

The problem is - you are using By.className(), but provide a CSS selector inside. Instead, use By.cssSelector():

WebElement writeSomething = driver.findElement(By.cssSelector("input.row0.col1.widget"));

Upvotes: 2

Related Questions