<table class="sc-fAEnHe ePMtc">
<tbody>
<tr class="sc-fAEnHe ePMtc">
<td classs"sc-jEECVv IBUtl">
</td>
</tr>
When I use Selenium's Find element by class it is able to find the element, I even tried replacing space (after e and r or v and I) with ".", "-" and "_" but it did not worked.
I Used The Code Below
try:
match_history_table = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CLASS_NAME, 'sc-fAEnHe ePMtc'))
)
except Exception:
print("Error Finding Match History Table")
driver.quit()
It always returned the Exception (EC is selenium.webdriver.support.expected_conditions)
>Note: Find Elements By Tag is not an Option For me
This is doc from By.CLASS_NAME found in selenium-java-bindings.
Find elements based on the value of the "class" attribute. Only one class name should be used. If an element has multiple classes, please use cssSelector(String).
Try with (By.CSS_SELECTOR, '.sc-fAEnHe.ePMtc'):
try:
match_history_table = WebDriverWait(driver, 30).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.sc-fAEnHe.ePMtc'))
)
except Exception:
print("Error Finding Match History Table")
driver.quit()
Referenses
json wire protocol
https://www.selenium.dev/documentation/legacy/json_wire_protocol/#sessionsessionidelement
class name Returns an element whose class name contains the search value; compound class names are not permitted.
W3C webdriver
And class_name not mentioned in W3C protocol, so it might become legacy
https://www.w3.org/TR/webdriver/#locator-strategies
when you search for class name value, notice that "sc-fAEnHe ePMtc" represent 2 class names separated by space, so you can search by "sc-fAEnHe" or by "ePMtc"
besides that make sure that presence_of_element_located doesn't require an element with height and width that greater then 0
Related
I'm new to Selinium. There is a table (inside another table) that I want to click each <td>click here</td> with a matching class value (note the text between opening and closing td will change but is irrelevant for matching purposes). The class value I'm trying to match is open. From here I learn the right way is with //*[contains(concat(" ", normalize-space(#class), " "), " open ")]
This seems to work but just for one random cell. How do I make it click all? I was planning on accomplishing this first and repeating the step, but it may be worth noting that the script should also do the same for class value available and not just open Is there away to do logical or?
TL;DR I want to click everything with <td class="open">...</td>
and
<td class="available">...</td> where ... is example text that will vary but should be ignored.
Get all elements and store them in a List, then iterate over them and click the buttons in sequence.
List<WebElement> list = driver.findElements(By.xpath("//*[contains(concat(" ", normalize-space(#class), " "), " open ")]"));
for(WebElement webelement : list) {
// webelement Click the button here
}
To get all elements, you should use driver.FindElements. Make sure it's what you used, not driver.FindElement?
Use following approach may help you -
List<WebElement> allElements = driver.findElements(By.tagName("td"));
for(WebElement element:allElements)
{
if(element.getAttribute("class").contains("open"))
{
// manipulate with <td> tag data
}
}
Please use following XPath:
//td[normalize-space(#class)="open" or normalize-space(#class)="available"]
This should match both <td class=" open ">...</td> and
<td class=" available ">...</td>
I have a span as below:
<div class="ag-cell-label">
<span class="glyphicon glyphicon-asterisk" title="This is a draft row. It can only be seen by you. "/>
</div>
I want to get the text "glyphicon glyphicon-asterisk". How can I do it.
The validation of the test case is to check weather asterisk is not present after clicking on save button.
Assuming you are using Java, You should try as below :-
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='ag-cell-label']/span")));
String class = el.getAttribute("class");
Hope it will help you...:)
This is a simple case of:
Locate the WebElement using a suitable locator strategy (Class, CSS, XPath etc) and assign it to a new WebElement object.
Use the .getAttribute(String arg) method with an argument of "class" to retrieve the required class value from the WebElement object instantiated in the first step and assign it to a new String object.
Use the .contains(String arg) method with an argument of "asterisk" to determine whether the "class" attribute retrieved in the second step contains the text "asterisk".
For example, consider the below html tags, where I need to get the exact index/position of the text, three
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
The expected value is '3'
You could do this:
from selenium import webdriver
driver = webdriver.Firefox()
# This is an actual bin with a test page in it.
driver.get("http://jsbin.com/wagonazipa")
# Obviously, this needs to be precise enough to find the tr
# you care about. Adapt as needed.
tr = driver.find_element_by_tag_name("tr")
# Find the element you care about. You might want to use td rather
# than *.
target = tr.find_element_by_xpath("*[. = 'three']")
# Get all the children of the row.
children = tr.find_elements_by_xpath("*")
# Get the index of target in the children list.
print children.index(target)
Python's implementation of Selenium is such that you can do comparisons between WebElement objects with == and thus index works. If you are working with a language that does not do this, you'd have to get the identifier that Selenium assigns to each WebElement object. On Python that's the .id property. In other languages you have a getId() or get_id() method to get the id. Then you can compare WebElement objects by identifier.
If the jsbin becomes inaccessible, this is the relevant HTML in it:
<body>
<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
</table>
</body>
Here is an example for the same using JAVA,
driver.get("http://www.indiabookstore.net/");
WebElement list = driver.findElement(By.xpath("//ul[#class='nav navbar-nav navbar-right']"));
WebElement li = list.findElement(By.xpath("*[. = 'Offers']"));
List<WebElement> children = driver.findElements(By.tagName("li"));
System.out.println(children.indexOf(li));
The HTML code is given attached, I do not want to use hard code xpath, the requirement is to make it generic:
<td bgcolor="#FFFFFF">
<input name="hotel_name_0" id="hotel_name_0" type="text" value="Hotel Creek" class="select_text" onfocus="disable_ctrlV()" onkeypress="return Nothingonly(event)">
</td>
Code:
public static boolean fncVerifyTextInColumn(WebElement gridObjWebElement,
String stringToValidate, int columnNumber,String colName) {
boolean flagTextinColumn=false;
ArrayList<WebElement> objRows;
ArrayList<WebElement> objCols;
ArrayList<WebElement> childElement;
objRows=(ArrayList<WebElement>)gridObjWebElement.findElements(By.tagName("tr"));
objCols=(ArrayList<WebElement>)objRows.get(0).findElements(By.tagName("td"));
if(objCols.get(columnNumber).getText().equalsIgnoreCase(colName)){
for(int index=1;index<objRows.size();index++){
objCols=(ArrayList<WebElement>)objRows.get(index).findElements(By.tagName("td"));
childElement=(ArrayList<WebElement>)objCols.get(columnNumber).findElements(By.xpath("//input"));
System.out.println(childElement.get(0).getAttribute("value"));
if(stringToValidate.trim().equalsIgnoreCase(childElement.get(0).getAttribute("value").trim())){
flagTextinColumn=true;
}
}
}
return flagTextinColumn;
}
Method Calling:
fncVerifyTextInColumn(objGrid,hotels,1,"Hotel Name");
I would use cssSelector [id^='hotel_name_'] to locate the element and then getAttribute() retrieve the attribute value
By css = By.cssSelector("[id^='hotel_name_']");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(css));
System.out.println(myDynamicElement.getAttribute("value"));
Notice the regex search of cssSelector here. With ^ I am skipping any dynamic number. Hoping that's the only element with hotel_name_someNumber on the page.
Just do
String attValue = driver.findElement(byAnyMethod).getAttribute("AttributeName");
Hope it helps
I think what you are looking for is this. (I'm assuming you know how to code, you just need a general direction so I'm going to leave out specific code.)
First, find the table the td is in. You might need to use an xPath for this or you'll need to assign an ID to the table so you can locate it.
Then once you have the table, do a FindElements to get the list of TRs under it.
Once you have the TRs, you can loop through them, grab the TDs under that and grab the TD at the index that has the INPUT you want the value of, get the INPUT and then get it's value.
Yep, lots of steps.
A shortcut may be to class all of the inputs you want the value for with a unique class and do a FindElements By className and loop through that list.
I try to get a href from an anchor according to linkText given with func param (for example here Westwood or Linda).
In the code below I changed classes values (cause of company info), but beside thats the code. For each <tr> the hrefs are identical for both <td>, but different from <tr> to <tr>
I get the 1st href with:
driver.findElemnt(By.xpath("//a[#class = 'class Name of a']").getAttribute("href")
or the same with:
xpath="//a[#class = 'class Name of a'][1]";
but when I go:
"//a[#class = 'class Name of a'][2]" or 3 or 4...
or when I use:
By.partialLinkText
It fails, so I never get beyond the 1st <td> of the first <tr>.
The Error: oopenqa.selenium.StaleElementReferenceException : Element
is no longer attached to the Dom.
The Elements are visible all the time, so its not a visibility problem.
This is the html:
<tr class="Class name for tr here">
<td headers="description _ CustomerList:lastName">
Westwood
</td>
<td headers="description _CustomerList:firstName">
Linda
</td>
</tr>
You're trying to retrieve the second anchor tag within the same parent (in the example document: table cells).
Instead of
//a[#class = 'class Name of a'][2]
query the second anchor tag in the whole document:
(//a[#class = 'class Name of a'])[2]
Some more examples how to use positional predicates (I omitted the class predicate here):
Anchor tags from all second table cells per row:
//td[2]/a
Second anchor tag per row:
//tr//a[2]