I am trying to locate some elements in Selenium and I have problem.
There are no normal elements like ID etc.
Issue no 1 --> should click to Continue button
<a _ngcontent-c3="" routerlink="cms" href="/cms">Continue</a>
Issue no 2 --> should click to Add button
<button _ngcontent-c11="" aria-label="Add user" class="action-btn add" type="button">
Issue 2 html screenshot
I tried using fidning elements by PartialLinkText, I tried to make my own CSS selector/xPath, but test case always fails...
One of unsuccessful solutions for Issue 1:
WebElement continueButton = driver.findElement(By.partialLinkText("Continue"));
continueButton.click();
Any suggestions?
Thanks.
You can find button by class and i hope using this xpath your problem will be
Here your possible xpath is //a[#routerlink='cms'][text()='Continue'] and //button[#aria-label='Add user']/i']
WebElement Continue = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#routerlink='cms'][text()='Continue']")));
Continue.click();
WebElement adduser = (new WebDriverWait(driver, 30))
.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#aria-label='Add user']/i")));
adduser.click();
Related
I'm trying to select an element based on its text contents. I am using XPath to achieve this.
I am just puzzled as this should work?
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I'll even copy the HTML code:
<div class="linkWrap noCount">Notifications <span class="count _5wk0 hidden_elem uiSideNavCountText">(<span class="countValue fsm">0</span><span class="maxCountIndicator"></span>)</span></div>
The div element has the words "Notifications" inside it. So why doesn't it work.
Go to this page on Facebook: https://www.facebook.com/settings
Use this chrome extension to highlight any area via xPath.
You have a space before the word Notifications:
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
You should also add a wait for element before trying to find the element:
WebDriverWait wait = new WebDriverWait(webDriver, timeoutInSeconds);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(), 'Notifications')]"));
WebElement link = obj.driver.findElement(By.xpath("//div[contains(text(), 'Notifications')]"));
I found the issue with the help of some amazing people in this community.
Ok, so my element was in an iFrame.
In order to access the element, I must first access the iFrame
WebElement iframe = obj.driver.findElement(By.xpath("//iframe[#tabindex='-1']"));
obj.driver.switchTo().frame(iframe);
Unable to click on webelement button
I tried to click on button by mouse movement but no success
my outer html is as below :
<button class="btn btn-alt btn-small" type="button" ng-click="ecdapp.uploadBlueprintModalPopup();">
Create
</button>
button xpath is:
//*[#id="page-content"]/div[3]/button
Not seeing the full page source it's hard to tell where your XPath expression is good or not, you can try locating the button using its text instead
//button[normalize-space(text())='Create']
the normalize-space() function is used to discard heading/trailing whitespaces
It might also be the case the button is not immediately available, I would recommend considering using Explicit Wait approach via WebDriverWait class
WebElement myButton = new WebDriverWait(driver, 10)
.until(ExpectedConditions
.elementToBeClickable(By.xpath("//button[normalize-space(text())='Create']")));
myButton.click();
the above code will try to locate the aforementioned button for 10 seconds and click it as soon as it will be present/visible/clickable. Otherwise it will fail with NoSuchElementException
May be the Xpath is wrong. Try the below xpath:
//button[contains(text(),'Create')]
As you can see on the screenshot this Xpath 100% works, if you still won't be able to click on that button, then problem is not with xpath. Let me know if its still fails.
By.xpath("//button[#class = 'btn btn-alt btn-small' and #type = 'button']")
Based on your comment:
I tried this code , but unable to click . element click intercepted: Element ... is not clickable at point (293, 97). Other element would receive the click: ... (Session info: chrome=74.0.3729.169)
I pretty sure I know whats your problem, before u click on this element, something going on the page:
It says - Other element would receive the click, means there is other element above(overlapping) your button(pop up window, page is greyed out(disabled while loading, Some JS running)), so when Selenium trying to click on your button its actually clicking on that blocking element.
Try to click after Thread.Sleep(); wait 5-10 sec.
If this is the case then u need to add condition before find your button to check that element that prevent from clicking on button is disappeared then u click on it.
Try JavaScript executors as below,
WebElement element = driver.findElement(By.xpath("<xpath of button>"));
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);
While automating in automationpractice.com,
Steps:
1.open "automationpractice.com/index.php?id_product=5&controller=product#/size-s/color-orange"
2. click "Add to Cart" button
3. Below add to Cart successfully pop-up displayed(Refer to below screenshot)
Now I cannot mark any elements in this pop up. I tried XPath which is inside the parent window with Div tag, but The error is ElementNotVisible. I tried it with driver.getWindowHandles(), but it shows that only 1 window exists(Parent window). Also tried with alert, but it says no alert exists.
This issue only occurs in the Chrome browser, for firefox , it works fine using xpath inside parent window with Div tag.
Below is the script I tried, can anyone kindly help on this? Thank you in advance!
The xpath for this pop up is "//*[#id="layer_cart"]/div[2]"
Set<String> windows = wd.getWindowHandles();
System.out.println("windows.size():"+ windows.size()); // the result is 1
There is no new window or alert on this page, only problem is your XPATH cotains " instead of ', alo need to wait a few seconds for that element to be visible:
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_product=2&controller=product");
driver.findElement(By.xpath("//form[#id='buy_block']//p[#id='add_to_cart']")).click();
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[#id='blockcart_caroucel']/li[1]")));
driver.findElement(By.xpath("//*[#id='blockcart_caroucel']/li[1]")).click();
This is the Button I want to press:
<button class="ProfileTweet-actionButton js-actionButton js-actionRetweet" data-modal="ProfileTweet-retweet" type="button">
The same kind of Button exists multiple time on the site.
I tried pressing it by doing this:
By byaXpath = By.xpath("//div[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]");
WebElement Element = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(byaXpath));
Element.click();
But after 10 secons I just get this error:
org.openqa.selenium.TimeoutException: Timed out after 10 seconds waiting for presence of element
Im kinda lost now. I tried diffrent solution to similar cases but nothing seems to work.
You have to put button instead of div at the start of your xpath.
"//button[contains(#class,'ProfileTweet-actionButton js-actionButton js-actionRetweet')]"
Below is the span that I'm trying to click. The number sequence in the id is changing dynamically.
<span id="actionsButton__o3uid5535_label" class="dijitReset dijitInline dijitButtonText" data-dojo-attach-point="containerNode,_popupStateNode">Actions</span>
So I tried to use..
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[contains(text(), 'Actions')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click()", el);
but no success.
Can someone please help me in finding solution for this issue