How can i click on a nested anchor href tag using selenium webdriver? - selenium

This is my html code:
YIKKU, TFYTUR
I want to click on the link name YIKKU TFYTUR, i have tried the following but nothing worked-
driver.findElement(By.partialLinkText("YIKKU, TFYTUR")).click();
driver.findElement(By.cssSelector("a[href*='Y']")).click();
can anyone please help me??

The only solution to these kind of Href tags are find the nearest "id" element, in my case was this-
<table id="resSearchResultsTBL">
then find this element using css selector:
WebElement guest = driver.findElement(By.cssSelector("table[id='resSearchResultsTBL']"));
and then find again in this element a sub element of "a href" tag:
guest.findElement(By.cssSelector("a[href*='guestProfile.do']")).click();
This worked perfectly for me.:)

Try -
WebElement link = driver.findElement(By.xpath("//a[#name=\"Y\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();
or
WebElement link = driver.findElement(By.xpath("//a[#target=\"sgr\"]"));
wait.until(ExpectedConditions.elementToBeClickable(link));
link.click();

Related

Unable to click on "know more..." link on the page given below using the selenium webdriver

page url: https://netbanking.hdfcbank.com/netbanking/
enter image description here
I tried using switch to frames from selenium webdriver, still it doesn't help me
Below is the code sample
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='nortonimg2']/div/span/following-sibling::span/a")).click();
You just need to switch to the frame and then click the proper element inside it.
The following code should work:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='welcomesec2']//a")).click();
The result is
You were almost there but the locator strategy for the link Know More... was slightly off as there are multiple elements with almost similar attributes:
Solution
You can use either of the following locator strategies:
Using partialLinkText:
WebElement frame = DriverManager.getDriver().findElement(By.cssSelector("frame[name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.partialLinkText("More...")).click();
Using cssSelector:
WebElement frame = DriverManager.getDriver().findElement(By.cssSelector("frame[name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("div#welcomesec1 span.lightbluecolor > a")).click();
Using xpath:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//div[#id='welcomesec1']//span[starts-with(., 'Your security is of utmost')]//following::span/a[starts-with(., 'Know') and contains(., 'More...')]")).click();
Although your XPath for 'Know More' link is correct, somehow web driver is not able to click on it. Try a different XPath for the same 'Know More' link, like below:
WebElement frame = DriverManager.getDriver().findElement(By.xpath("//frame[#name='login_page']"));
DriverManager.getDriver().switchTo().frame(frame);
DriverManager.getDriver().findElement(By.xpath("//*[#id=\"nortonimg1\"]/div[2]/span[2]/a")).click();

Selenium-Unable to identify the search box element using xpath

WebElement SearchField =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/input"));
SearchField.sendKeys("my name");
Thread.sleep(5000);
WebElement SearchButton =
driver.findElement(By.xpath("/html/body/div[2]/div[3]/form/div[1]/div[1]/div[3]/center/input[1]"));
SearchButton.submit();
Thread.sleep(3000);
driver.quit();
In above code I have tried to select the search box in google.com and enter "my name" as the value.
but it says xpath element not found.
So I took the "name" instead of xpath element and it worked.
WebElement SearchField = driver.findElement(By.name("q"));
How can I run this using xpath?
Why you want to switch to xpath if name works fine ?
There are lot of advantage if you use ID, NAME, LINKTEXT, PARTIALLINKTEXT, CSSSELECTOR, CLASSNAME and lastly XPATH :
Anyway you can try this xpath :
//input[#name='q']
this is a relative xpath. The one you are using is absolute xpath. try to avoid that.
The unique XPath for the search input element you are looking is:
//input[#name='q' and #type='text']

Not able to click button(WebElement) in Selenium Webdriver 3.7

I was trying to automate add to cart functionality in the following website, but 'Add To Cart' button is not getting clicked, though element is identified and code has been written to click on the button using Actions class and Javascriptexecutor.
Site: https://redmart.com/sales
Button: Add To Cart
Selenium Code:
WebElement element4 =
driver.findElement(By.xpath("//article[#id='contentSection'] //div[#class='productShelf']//ul/li[1]"));
WebElement element5 = element4.findElement(By.xpath("div[3]/div/a/span"));
actions = new Actions(driver);
actions.moveToElement(element4).moveToElement(element5);
Thread.sleep(3000);
actions.click();
actions.build().perform();
Can someone please suggest a solution which will click on Add To Cart button and added element should be displayed in cart as well?
Your xpath is not correct because it doesn't select the button.
Try this xpath "//li[#data-id='88800134']/div[3]/div/a" and let me know if this solve the problem.
driver.findElement(By.xpath("//li[#data-id='88800134']/div[3]/div/a")).click();
Later Edit:
WebDriver driver = new ChromeDriver();
driver.get("https://redmart.com/sales ");
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
WebElement element =driver.findElement(By.xpath("//li[#data-id='88800134']/div[3]/div/a"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
I really think you are selecting the wrong element:
browser.find_element_by_xpath(".//*[#id='contentSection']/div/article/div[2]/div/div/ul/li[1]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[#id='contentSection']/div/article/div[2]/div/div/ul/li[2]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[#id='contentSection']/div/article/div[2]/div/div/ul/li[3]/div[3]/div/a").click()
browser.find_element_by_xpath(".//*[#id='contentSection']/div/article/div[2]/div/div/ul/li[4]/div[3]/div/a").click()
Anyway have a look on my answer in this post to ensure that you are getting the correct information ;)
python selenium click on button
Please try this xpath:
//ul[contains(#class,'productList')][1]//li[1]//a[contains(#class,'Button')]
It is for first ,,Add to Cart'' button on list.
If you want to click other product, just change index of product list or li tags in xpath.
Please also try clicking without using Actions.
driver.findElement(By.xpath("//ul[contains(#class,'productList')][1]//li[1]//a[contains(#class,'Button')]").click();
It can be an Actions issue.
I hope it helps!
Try with the below xpaths :
//ul[contains(#class,'productList')][1]//li[1]//a[contains(#class,'Button')]/span
This one is for the first item.
(//span[text()='Add to cart'])[1]
Try changing replacing your WebElement element5 to the code below:
WebElement element5 = element4.findElement(By.xpath("/div[3]/div/a/span/parent::node()"));
because it seems like you are trying to click a span element, that's why it's not doing anything

Selenium clicking a sub menu

I am Really stuck for the past two days here. I am trying to click sub menu and when I try to click sub menu I get an errors as like the following
Element not found for the sub menu.
I have tried below code
WebElement element = driver.findElement(By.id("x-menu-el-P46915788081933044__folderbrowser_PJL"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);
HTML Code
<li id="x-menu-el-P46915788081933044__folderbrowser_PJL" class="x-menu-list-item">
<a id="P46915788081933044__folderbrowser_PJL" class="x-menu-item" href="javascript:var abc = doNothing()" unselectable="on" hidefocus="true">
<img id="ext-gen926" class="x-menu-item-icon " src="netmarkets/images/import.gif">
<span id="ext-gen927" class="x-menu-item-text">Upload Documents from Compressed File</span>
Instead of using the ID you should probably use the class name.
WebElement element = driver.findElement(By.ClassName("x-menu-list-item"));
or you could try using the css selector
WebElement element = driver.findElement(By.cssSelector("li[class='x-menu-list-item']"));
Since the above return multiple items you could just use to return the exact element that you need:
WebElement element = driver.findElement(By.linkText("Upload Documents from Compressed File"));
1st click on the menu and then try the following statement -
driver.findElement(By.xpath("//li[#class='x-menu-list-item']//span[contains(text(),'Upload Documents from Compressed')])).click();
or directly try this -
driver.findElement(By.xpath("//span[contains(text(),'Upload Documents from Compressed')])).click();
I guess mostly the error is due to the span name spaces in between words, if the above dont work, pls attach a screenshot or give some details of the html code, so we can try more options, all d best.

How to verify if the image in Selenium using Webdriver

How would I verify the image is displaying is the correct path/name in Selenium using WebDriver?
I started using this code but not sure :
string _active = "<img style="display: ;" alt="Active" src="../App_Themes/Default/images/check.png"/>";
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
I would like to clarify to you that this code
driver.FindElement(By.XPath("//*[#id='ctl00_ContentPlaceHolder1_AddeCardControl1_gv']/tbody/tr[11]/td[7]/img")).Text.Contains(_active);
does not give you the html code for the image tag but a IWebElement object. And you can read the various attributes of this WebElement by using the GetAttribute method.
You will have to get the src attribute of the img tag you are looking for by locating the image(webelement) by xpath and then
IWebElement element = driver.FindElement(By.XPath("Your xpath"));
string path = element.GetAttribute("src");
Now you can verify the path of your image. Hope this helps you.