How to manage pop-up using Selenium Web Driver in github - java

I'm having troubles with managing of the pop-up in the github.
Test case which I'd like to automate is:
1. Go to https://github.com/YOUR_USER/YOUR_REPO/settings
2. Click "Delete Repository" button (pop-up appears)
3. Fill in the name of your repository into the input in pop-up
4. Click "I understand the consequences, delete this repository" the button in pop-up
I don't know how to find the element in pop-up in the 3d step.
When I'm just tryin' to do this, webdriver fails to find element
driver.findElement(By.name("verify")).sendKeys(repoName);

Following locator (css selector) should work:
#facebox .input-block"
Use this locator as follows:
driver.findElement(By.cssSelector("#facebox .input-block")).sendKeys(repoName);
Another css selector that you can use is as follows:
#facebox [name=verify]
driver.findElement(By.cssSelector("#facebox [name=verify]")).sendKeys(repoName);

There are more that one input tags with the same name attribute value "verify". You need to select the second one and not the first. You can try something like this:
driver.findElements(By.name("verify"))[1].sendKeys(repoName);

Related

cant find the element using classname in selenium java

I'am using selenium framework to test my own website. Im trying to click on specific icon which using anchor tag. I have java selenium code to click but couldn't click. Tried many xpath, css selectors, class name and names. but didn't worked. But can run the script and it is opening the chrome and navigating to entered domain but the clicking option is not working
above code I need to click nav-twitter class anchor option . which will create another tab in chrome to show my twitter page. but after running the app .it is only navigating to the page domain and nothing works.
So, This is my code where I have added. until the maximize everything works but not the anchor tag
This kind of error im getting when running the script in chrome. Please anyone let me know where I have been wrong here or is there are any way to make it happen. Thanks in advance.
Whenever you are initiating webdriver make sure to add implicit wait statement, so it can wait for sometime before looking for objects. Add below statement right after chromedriver initialization and your code should work without any issues.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Your locator is slightly off. In your code you are looking for an element with tagName as twitter which doesn't exists. Instead you can use either of the following locator strategies:
Using cssSelector:
WebElement navTwitter = driver.findElement(By.cssSelector("a.nav-twitter[name='twitter']"));
navTwitter.click();
Using xpath:
WebElement navTwitter = driver.findElement(By.xpath("//a[#class='nav-twitter' and name='twitter']"));
navTwitter.click();
You have used wrong selector/locator here. "twitter" is not tag name, Tag name is the value from less than symbol to the first space or greater than symbol. Words like div, p, a, span are the tag names.
So here a is the tag name and name, class, href are attribute which you can use while forming xpath or css locators. So you can create multiple locators here:
By using class name attribute
By.className("nav-twitter");
By creating xpath using name attribute
By.xpath("//a[#name='twitter']");]
By creating xpath using class attribute
By.xpath("//a[#class='nav-twitter']");
By creating css locator using class attribute.
By.cssSelector("a[class*='nav-twitter']");
By using dot symbol in css selector with class name
By.cssSelector("a.nav-twitter");
Similarly you can create css selector with name too. Since there are multiple tags, so you can not use a tag directly and If you want to use By.tagName("a") then it will click on first tag present on the page
Note: We use dot with class name and Hash (#) with Id name while forming css selectors.

How to check if Selenium clicks on element successfully?

In my java and Selenium project, I have the below method to click on a specific element
public void click(String xPath) {
driver.findElement(By.xpath(xPath)).click();
}
Currently, i have an element that it does not react on my click! So, i need to make sure, if selenium really successfully click on this element.
Point: There is no error during the runtime. So, i think it can find the element. But, it is a surprise for me, that there is no effect of click!
More details:
I am trying to automate this page. You can find the complete code in this repository. You need to take a few steps till you reach to the point that i really have the problem. That's why i share the repository, since the page is not directly accessible (sorry for that).
When i am in the https://hello.friday.de/quote/selectFuelType, (please find the image)
I am not able to click on the item (Benzin) with the below xpath:
final String fuel = "//*[#id=\"root\"]/div/div[3]/div/div[2]/div/div/form/div[2]/div[2]/button[1]";
During the runtime, there is no error, so, i expect that I can successfully click on the element, or successfully get the current url as /selectFuelType but none of them are working.
The problematic method is the i_am_asked_to_specify_the_Fuel_Type_of_the_car() in the RegisterInsuranceSteps class.
Your xpath can be much simpler and stabler. Try this:
final String fuel = "//button[contains(., 'Benzin')]"
See more details about contains method at MDN web docs or check this SO answer to see how to check against attributes or tag names.
Regarding verifying if button was really click, I think it's enough if your next test step fails. In your case it would be choosing the engine power.
On the website https://hello.friday.de/quote/selectPrecondition the first option Das Auto ist schon versichert is selected by default. So click() the second option Das Auto wird noch zugelassen oder umgemeldet you you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:
cssSelector:
WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"div[class^='RadioButtonListField__container--'] button:nth-child(2)"))).click()
XPATH:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[starts-with(#class, 'RadioButtonListField__container--')]//following-sibling::button[1]"))).click();
Note: These are relative locators as per the relevant HTML and are not hard-coded to the exact text of the options.
Firstly if selenium doesnt click then try to use alternate xpath. You can also use css selectors.
Secondly if you want to check if click has happened and next page is loaded then you can simply check change in title or you can check if some element has loaded from next page.

How to click a button where the class is the same for another options?

I am trying to click a button from a list but this button has the same class than others in the list because they have the same name (btn ban-red) so how can I click it if in the inspect I have this information:
<a class=“btn ban-red” data-track-event=“navigate” data-track=name=“Jobylon” - Quality Engineer” href=“https://emp.jobylon.com/jobs/16654-f/” target=“_blank”>View job/a>
The inspect is copying this xpath:
/html/body/div[1]/div[4]/div/div/div/div[3]/div/div/div/div[1]/section/div/div[2]/div[1]/div[1]/article[14]/a
But it is not working
I also created my own xpath this way:
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[#data-track-name=‘Jobylon - Quality Engineer’]"))).click();
But is not working either
I am using Selenium with java and I am in a Macbook, thank you for your help.
Absolute xpath not recommended. You can try using relative xpath.
Locate based on element text
driver.findElement(By.xpath("//a[contains(.,'View job')]")).click()
Locate using combination of element attribute if classes are not unique
driver.findElement(By.xpath("//a[#class='btn ban-red'][#data-track-event='navigate']")).click()
OR
driver.findElement(By.xpath("//a[#class="btn ban-red"][#href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Better to use CSS selector as its faster then xpath. So you try like
driver.findElement(By.cssSelector("a[class='btn ban-red'][data-track-event='navigate']")).click()
OR
driver.findElement(By.cssSelector("a[class="btn ban-red"][href='https://emp.jobylon.com/jobs/16654-f/']")).click()
Still facing some issue like element not visible or no such element then try with explicit wait conditions until your element gets visible or clickable.

Selenium WebDriver "java", I can not click on button on footer

The scenario is:
Try to add experience in linkedin.
Then click on save button to save the added experience.
The below is html code for this button:
<button class="pe-form-footer__action--submit form-submit-action Sans-15px-white-100%" type="submit">
Save
</button>
I am trying to find it by xpath using:
#FindBy (xpath = "//*[contains(text(), 'Save')]")
WebElement saveExperienceButton;
Following screenshot may help:
I will appreciate your help.
if you do not mind css/xpath selectors that do not look very elegant, you can always open up Chrome developer tools on the website you wanna test with Selenium, mark the DOM elements you wanna access and in the context menu choose 'Copy xpath' or 'copy selector':
Try this xpath:
(//*[text()='Save'])[2]
On my profile there are 2 Save buttons - the second one is the skill save. Also, you might want to check this question for the contains syntax.
Creating an XPath using text is a less preferable way. instead of that use other attribute value which is unique.
for ex: in your case
//footer//*[contains(#class, 'form-submit')]

I want to perform click action on button present under "svg" layout using selenium library

I want to perform click action on a button present under .svg layout using Selenium (Java bindings).
For example, I want to click on the button element, but every time I try to find an element by xpath, I get exception `enable to locate element
I read that with Selenium its tricky to click on element present under the .svg.
Is there anybody who knows a solution because, I haven't found a suitable solution on the net by myself.
Find below HTML code look likes this way:
My code:
List <WebElement> frame1=driver.findElements(By.xpath("//iframe[contains(#id,'-06636000002Pb2L')]"));
System.out.println(frame1.size());
System.out.println(frame1.get(0).getAttribute("title"));
driver.switchTo().frame(0);
ElementaryOperations.Sleep(3000);
System.out.println("New relation frame found");
driver.findElement(By.cssSelector("#newrel")).click();
After switching to frame successfully, I am not able to click on the element present under the SVG layout. Please refer to the attached screen shot(link)
this is how you should be able to identify SVG node and then sub nodes of that.
//*[local-name() = 'svg']
use this code
driver.findElement(By.id("rfb")).click();
try using class attribute of the button using CSS Selectors:
driver.findElement(By.cssSelector(".btn.btn-primary")).click()

Categories

Resources