Selenium Webdriver - How to select checkbox, if it has no id? - java

So I have this Google Docs survey and there are checkboxes which I want to check with Selenium Webdriver automatically. I tried to locate the element ID but it doesn't have one, only a class name.
<label class="docssharedWizToggleLabeledContainer freebirdFormviewerViewItemsRadioChoice">
<div class="exportLabelWrapper">
<div class="quantumWizTogglePaperradioEl docssharedWizToggleLabeledControl freebirdThemedRadio freebirdThemedRadioDarkerDisabled freebirdFormviewerViewItemsRadioControl" jscontroller="EcW08c" jsaction="click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseleave:JywGue; touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd; focus:AHmuwe; blur:O22p3e; keydown:I481le; contextmenu:mg9Pef" jsshadow="" aria-label="Männlich" tabindex="0" data-value="Männlich" aria-describedby=" i5" role="radio" aria-checked="false" aria-posinset="1" aria-setsize="3">
<div class="quantumWizTogglePaperradioInk exportInk"></div>
<div class="quantumWizTogglePaperradioInnerBox"></div>
<div class="quantumWizTogglePaperradioRadioContainer">
<div class="quantumWizTogglePaperradioOffRadio exportOuterCircle">
<div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div>
</div>
</div>
</div>
<div class="docssharedWizToggleLabeledContent">
<div class="docssharedWizToggleLabeledPrimaryText">
<span dir="auto" class="docssharedWizToggleLabeledLabelText exportLabel freebirdFormviewerViewItemsRadioLabel">Männlich</span>
</div>
</div>
</div>
</label>
<div class="quantumWizTogglePaperradioRadioContainer">
<div class="quantumWizTogglePaperradioOffRadio exportOuterCircle">
<div class="quantumWizTogglePaperradioOnRadio exportInnerCircle"></div>
</div>
</div>
"Männlich" is the text of a label which belongs to the checkbox I want to mark, but not the checkbox as an element by itself.
I have tried
driver.findElement(By.name("name"))
driver.findElement(By.id("id"))
in this case.

You have to work with xpath: https://www.w3schools.com/xml/xpath_syntax.asp
Something like driver.findElement(By.xpath("//div[#class='docssharedWizToggleLabeledContent']/div")) should work.

Related

Selenium - How to get following sibling?

So I want to target a textbox that appears in a list that comes after the label "First Name" and send a first name to the box, but can't seem to be able to target the textBox...
What I've tried:
WebElement firstNameLocTry = driver.findElement(By.xpath("//label[text()='First Name']/following-sibling::div"));
firstNameLocTry.sendKeys(firstName);
What the li look like:
<li class="WPTO WKVO" role="presentation" data-automation-id="formLabelRequired">
<div class="WBUO WETO WDUO">
<label id="56$551056--uid24-formLabel" data-automation-id="formLabel" for="56$551056--uid24-input">First Name</label>
<div class="WEUO wd-c8594868-6b31-4526-9dda-7d146648964b" aria-hidden="true">First Name</div>
</div>
<div data-automation-id="decorationWrapper" id="56$551056" class="WFUO">
<div class="WICJ">
<div class="WMP2 textInput WLP2 WJ5" data-automation-id="textInput" id="56$551056--uid24" data-metadata-id="56$551056" style="visibility: visible;">
<input type="text" class="gwt-TextBox WEQ2" data-automation-id="textInputBox" tabindex="0" role="textbox" id="56$551056--uid24-input" aria-invalid="false" aria-required="true">
</div>
</div>
</div>
</li>
Any reason my sendKeys just leads to Element not interactable?
The attached HTML code Produce below out put
With the given XPath points to the second "First Name" Div [below pic], when you perform sendKeys, it is obvious that the error "Element not interactable" will be thrown.
Try with below two Xpaths,
1. //label[text()='First Name']//parent::div/following-sibling::div
2. //label[text()='First Name']//parent::div/following-sibling::div//input
Please use following xpath:
//div[text()='First Name']
or try:
//*[contains(text(),'First Name')]

How to click dynamic element inside span class

I'm pretty new in Selenium and UI automation. Have some problem with clicking on a dynamic element inside span class. So this id everytime changes for each of 3 drop-down elements. So each class for each this element the same which is create the problem as well.
So I need change the value for id="react-select-2585057--value-item"
<div class="field loan-selection">
<label class="field__body">
<div class="field__label">Verwendung
<!-- -->
</div>
<div class="field__control">
<div class="Select customSelect has-value Select--single">
<div class="Select-control">
<span class="Select-multi-value-wrapper" id="react-select-2585057--value">
<div class="Select-value">
<span class="Select-value-label" role="option" aria-selected="true" id="react-select-2585057--value-item">Freie Verwendung</span>
</div>
<div aria-expanded="false" aria-owns="" aria-activedescendant="react-select-2585057--value" aria-disabled="false" class="Select-input" role="combobox" style="border:0;width:1px;display:inline-block" tabindex="0"></div>
</span>
<span class="Select-arrow-zone">
<span class="Select-arrow"></span>
</span>
</div>
</div>
</div>
</label>
</div>
As per the HTML to invoke click() on the element with dynamic id as id="react-select-2585057--value-item" assuming this element will always be a descendent of the node <div class="field__label"> you need to induce WebDriverWait for the desired element to be clickable and you can use the following solution:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[#class='field loan-selection']//div[#class='field__label' and contains(., 'Verwendung')]//following::div[1]//span[#class='Select-value-label' and starts-with(#id,'react-select-')]"))).click();

parse data of certain tag which is before a particular class

I need parse data from web page by tag ("p"). I try like this:
Elements content = document.getElementsByTag("p");
for(Element el : content) {
System.out.println(el.text());
}
And it's work fine. But I get superfluous data.
For example:
<div class="DicCellTerm">
<h1>Impossible</h1>
<div class=des>
<p class=par2><span class=hint><em>smth</em></span></p>
<p class=par2>1) (<em>with</em>) all, do</p>
<p class=par2>2) <span class=hint><em>text</em></span> some words</p>
<p class=par3>it is impossible</p>
</div>
</div>
</div><!--DicCell end-->
<div align="center" class="AdContent" id="adcontentnoprint">
<div class=SharedItems>
<div class=DicCellParent>
<span class=LinkOtherDic>+ dictionary <strong>impossible</strong> - translate</span>
<div class=DicCellOther id=diccellothershow>
<h2>impossible</h2>
<div class=des>
<p class=par1>1) important, is</p>
<p class=par1>what</p>
<p class=par1>2) true, false</p>
</div>
</div>
<!--DicCellOther end-->
</div>
<!--DicCellParent end-->
<div class=DicCellParent>
<span class=LinkOtherDic>+ translate <strong>important</strong> - dictionary</span>
<div class=DicCellOther id=diccellothershow>
<h2>importnant</h2>
<div class=des>
<p class=par1>1) müim, emiyetli; emiyet bar</p>
<p class=par1>it is very important - bu pek müimdir, bunıñ büyük emiyeti bar</p>
<p class=par1>2) qopayıp, qabarıp</p>
</div>
</div>
<!--DicCellOther end-->
</div>
<!--DicCellParent end-->
</div>
<!--SharedItems end-->
I need to get data by tag "p" before class SharedItems.
I tried parse data by class "DicCellTerm" and I get properly data. And all data is written in one line, but I need to get data as on web page.
Elements elements = document.select(".DicCellTerm p");
This grabs all p inside the .DicCellTerm class, then you can iterate over elements. Here is a link to all possible selectors in jsoup, this is where i get most of my help =)
https://jsoup.org/apidocs/index.html?org/jsoup/select/Selector.html

How to Access Model Dialog Box content developed in Bootstrap using Selenium Web Driver and Java

I am trying to access model dialogue box (developed in Bootstrap) using Selenium Web Driver in Java, I am unable to do so. Here i wan tot access Title, Content and Button.
One more point is here xPath is dynamic, every time xPath is generating differently. I know that can be solved by partially matching the xPath.
So help me to solve this problem. Here I am attaching image of my model box design.
Here is code snippet
Here is code
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<div class="bootstrap-dialog-header">
<div class="bootstrap-dialog-close-button" style="display: none;">
<button class="close">
×
</button>
</div>
<div class="bootstrap-dialog-title" id="5f663313-d82d-4ab8-9712-6665e80a3654_title">
Member Registration
</div>
</div>
</div>
<div class="modal-body">
<div class="bootstrap-dialog-body">
<div class="bootstrap-dialog-message">
Sample Message Sample Message Sample Message Sample Message Sample Message Sample Message.
</div>
</div>
</div>
<div class="modal-footer">
<div class="bootstrap-dialog-footer">
<div class="bootstrap-dialog-footer-buttons">
<button class="btn btn-primary" id="353ab962-44d3-497f-8318-6db703b460d8">
Ok
</button>
</div>
</div>
</div>
</div>
</div>
//div[#class="bootstrap-dialog-title"] for Title
//div[#class="bootstrap-dialog-message"] for message
//div[#class="bootstrap-dialog-footer-buttons"]/button for buttton
driver.switchTo().activeElement();
Thread.sleep(5000);
driver.findElement(By.xpath("//button[contains(text(),'"+data+"')]")).click();
This Solve My Problem ,
I also wants to access Message but i am unable to access

How to click a link with Tag name <a class </a> using Java in Selenium WebDriver

I have below HTML code, where I have different <'li' class> tags under the <'ul'> tag. Within the <'li'> class tag, I will have tag which needs to be clicked. These are results of some search in webpage, where all the items will be displayed as a list with pagination.
I need to verify if this exist? and if it exists in 3 or 4th Index on the page, I need to click that link. Please let me know how I can get all these items into array and loop them and click any product available at random index.
<ul id ="e1ResultsList">
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<div class="banner">
<div class="text">High Offer</div>
</div>
</div>
<span class="e1ListingImages">
<a class="`jsListingURL`" `href`="/event?`eventid`=7844">
<div class="e1SellerLogo e1Hidden"></div>
</span>
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/event?`eventid`=7844">vehicles and buses</a>
</div>
<span id="7844" class="" style="width: 100%;">enter code here
<div class="e1EventDetails">
<span class="e1EventInfo">
<div class="e1EventDescription">
<div class="e1EventID">
<div class="e1ListingCount">
<div class="e1ClosingDate">
<div class="e1EventHighlights">
</span>
</div>
</span>
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<span id="7846" class="" style="width: 100%;">
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon ribbon-white">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=310786">2009 International 9200i Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="310786" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>
<li class="e1ListingItem e1GrayShadow e1WhiteGradient e1ListingItemShow">
<div class="ribbon e1Hidden">
<span class="e1ListingImages">
<span class="e1ListingDetails">
<div class="e1ListingTitle">
<a class="jsListingURL e1ListingTitleLink" href="/listing?listingid=312207">2009 Mack CXU613 Vision Truck Tractor</a>
</div>
<span class="e1ListingInfo">
<span id="312207" class="e1ListingOffer" data-lastupdated="1430388219706">
</span>
</li>
</ul>
You could get a List of the 'a' tag WebElements on the page using the following:
List<WebElement> aTagsList = driver.findElements(By.tagName("a"));
You can then iterate over aTagsList as needed.
Edit:
To click the 4th 'a' tag WebElement in aTagsList, you can use the following:
WebElement requiredElement = aTagsList.get(4);
requiredElement.click();
Is it always the 4:th item you want to press?
If not, you have to know what you want to click. Either it is depending on the text or the "listingid".
driver.findElements(By.cssSelector("a[href*='310786']"));
or
driver.findElement(By.cssSelector("a:(*'2009 International 9200i Truck Tractor'*)")); );

Categories