How do I perform click action on a button with no "id".
<a class="ui-button altbuttonfix" onclick="mojarra.jsfcljs(document.getElementById('productionreporttoolbar-productionReportToolBarForm'),{'productionreporttoolbar-productionReportToolBarForm-j_idt43':'productionreporttoolbar-productionReportToolBarForm-j_idt43','userId':'11448','reportName':'','statementName':'productionReport.productionReport','token':'','productionReportId':'1000274'},'report');return false" style="" href="#">
View
It's a class type. Any help is appreciated. Thank you.
Why not just use the class value? I use that as a locator all the time:
Webdriver driver = new FirefoxDriver();
WebElement e = driver.findElement(By.className("ui-button altbuttonfix"));
e.click();
You can use the class to identify this button in this case it would be
css=a.ui-button.altbuttonfix
Related
I have this html tree:
<div class="css-1dbjc4ns" data-testid="test-modal"></div>
<div class="css-1dbjc4n" data-testid="test-icon">
<div class="css-1dbjc4n"><div class="css-1dbjc4n"</div><div class="css-1dbjc4n r-
1wbh5a2 r-1peese0 r-6uxfom r-mbgqwd r-11c0sde r-1udh08x">
<div dir="auto" class="css-901oao ">This item is not yet available</div>
I am trying to locate the text "This item is not yet available".
This is my xpath:
#FindBy(xpath = "//div[contains(#data-testid, 'test-modal-container')"//contains(text(),This item is not yet available));
private WebElement itemComingSoonalert;
I am getting no such element present exception, can anyone please help? Thank you.
Try this xpath :
#FindBy(xpath = "//div[#data-testid='test-icon']/descendant::div[#dir='auto']");
private WebElement itemComingSoonalert;
or as request by OP :
#FindBy(xpath = "//div[#data-testid='test-icon']/descendant::div[#dir='auto' and text()='This item is not yet available']");
private WebElement itemComingSoonalert;
HTML:
"Add to cart" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="ProductPrimaryCTA-cta_add_to_cart_button" type="button"><span class="button-wrapper"><span class="spin-button-children">Add to cart</span></span></button>
"Get in stock alert" object
<button class="button spin-button prod-ProductCTA--primary button--primary" data-automation-id="button" data-tl-id="cta_oos_button" aria-expanded="false" aria-label="get in stock alert" role="button" tabindex="0" type="button"><span class="button-wrapper"><span class="spin-button-children">Get in-stock alert</span></span></button>
I do not want to click "Add to cart". I just want to store locator info in WebElement "addToCart". Because objects have very identical properties, I chose this unique property "data-tl-id" but did not work.
Web Driver
WebElement addToCart = driver.findElement(By.xpath("//button[#data-tl-id='ProductPrimaryCTA-cta_add_to_cart_button']"));
Below worked for me but both above objects have the same class name so i cant use the class name.
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
I also tried below ways but none worked for me.
WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
WebElement addToCart = driver.findElement(By.xpath("//span[contains(#text,'Add to cart')]"));
I get error "No such element: Unable to locate element: ". How to locate the "Add to cart" object?
Your xpath is wrong WebElement addToCart = driver.findElement(By.xpath("//span[text()='Add to cart'"));
Try below solution
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[text()='Add to cart']")));
System.out.println("Printing "+webElement.getText());
Or you can also try xpath with contains
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement webElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(text(),'Add to cart')]")));
System.out.println("Printing "+webElement.getText());
since this xpath worked for you,
WebElement addToCart = driver.findElement(By.xpath("//span[#class='button-wrapper']"));
Use this to uniquely identify WebElement
driver.findElement(By.xpath("//button[contains(#data-tl-id,'cart')]/span[#class='button-wrapper']"))
you can also use this xpath
//span[contains(text(),'Add to cart')]
I have the HTML content as below:
<input type="radio" name="radioButton" value="on" id="radio1" tabindex="0">
<input type="radio" name="radioButton" value="on" id="radio2" tabindex="0">
<input type="radio" name="radioButton" value="on" id="radio3" tabindex="0">
Reference:
How to use OR condition for Keywords in Robot Framework?
I am currently trying the logic mentioned in above link.But it's not most relevant to my scenario.
I would like to fetch and select the radio button using it's type, name and id or class.
I want to achieve something like below:
Select Radio Button if //input[#type="radio"] AND //input[name="radioButton"] AND //input[#id="radio2"]
You don't need an if statement. Simply put all of your conditions in a single xpath. For example:
select radio button //input[#type='radio' and #name='radioButton' and #id='radio2']
Though, if the id is unique then you don't need all that. If id is unique you can use something like:
select radio button //input[#id='radio2']
You can do the next
WebDriver driver = new FirefoxDriver(); //it can be any driver, not specifically firefox
WebElement radioButton = driver.findElement(By.xpath("//input[#type='radio' and #name='radioButton' and #id='radio2']");
radioButton.click();
The above function will look for the element and click it. And as #Bryan says, if the id is unique you can look for the element only by its id.
WebDriver driver = new FirefoxDriver(); //it can be any driver, not specifically firefox
WebElement radioButton = driver.findElement(By.id("#id='radio2']");
radioButton.click();
I am new to Selenium. I am trying to click a sub menu option which has the following code:
<div class="nav-level nav-level-open">
<ul id="menu_0af0fc80e81924533d028c395adb60e8">
<li class="nav-item">
<a class="" href="?s=jobs&ss=jobs&mode=list">
<i class="icn-next" aria-hidden="true"></i>
<span> All Jobs - jobs/ internships/ campus interviews</span>
</a>
</li>
I am trying to click the button here labelled, "All Jobs - jobs/ internships/ campus interviews". Being a menu, there are multiple such links with the same class name. I would prefer locating the element with Css locator or Xpath. Can someone please help me?
Try the below xpath, i am assuming the string "All Jobs - jobs/ internships/ campus interviews" is not repeated in webpage
driver.findElement(By.xpath("//span[contains(text(),' All Jobs - jobs/ internships/ campus interviews')]"));
you can use above strategy in finding target exlements. if the element's display string is not dupicated in webpage
Intall the FirePath Plugin in firefox first and then inspect the button using the firepath. Try with the following code -
driver.findElement(By.xpath("//copy paste the xpath here")).click(); //driver is the reference of WebDriver interface.
driver.FindElement(By.XPath("//ul[#id='menu_0af0fc80e81924533d028c395adb60e8']/li/a")).Click();
Try out this:
WebDriver driver = new FirefoxDriver();
WebeElement element ;
element = driver.findElement(By.xpath("//*[ul[#id='menu_0af0fc80e81924533d028c395adb60e8']/li/a");
driver.click;
Try this :
driver.findElement(By.xpath("//span[contains(text(),' All Jobs - jobs/ internships/ campus interviews')]"));
i'm using selenium, but unable to find the button below using findElement(By.xpath ... Could someone provide some help? Thanks very much!
<button type='button' class='pzhc' data-ctl data-click='[["processAction", ["LookUpPreview","true",":event","","Rule-HTML-Section"]]]' ><div class='pzbtn-lft' ><div class='pzbtn-rgt'><div class='pzbtn-mid' data-click='...'><img src='webwb/zblankimage.gif' class='pzbtn-i'/> Lookup</div></div></div></button>
Use the class name:
WebElement element = driver.findElement(By.className("pzhc"));