Below is my code:
//<span class="ui-button-text">Export</span>
I tried all levels to click EXPORT button using below code and it won't work at all. Please advise..
Driver.findElement(By.xpath("//span[#class='ui-button-text']")).click();
Driver.findElement(By.xpath("Driver.findElement(By.xpath("//span[#class='ui-button-text']\")).click();
xpath is not working! any other ways are appreciated..
As per the HTML you have shared to click on the element with text as Export you can use either of the following solutions:
cssSelector:
Driver.findElement(By.cssSelector("span.ui-button-text")).click();
xpath:
Driver.findElement(By.xpath("//span[#class='ui-button-text' and contains(.,'Export')]")).click();
Related
I am new to Selenium but I have a question. On the URL
I want to click the tab "Nyckeltal" (at top of the page) with the
element:
<a class="instrument-table-tab-menu__tab js_tab-menu__tab" data-target="table_2">Nyckeltal</a>.
Currently I can click the tab using the code:
String xpath ="/html/body/div/div[4]/div/div[4]/div[1]/div[1]/nav/a[3]";
driver.findElement(By.xpath(xpath)).click();
But I guess there is a better way to do this?
I guess the path is regular changed because of ads and therefor I think using xpath is not particular good.
My question is there a better way to click "Nyckeltal" than using xpath with Selenium and if there is, how do I write?
You can use below xPath to click that tab.
//a[contains(#class,'instrument-table-tab-menu') and text()='Nyckeltal']
In these type of cases, you need to create a dynamic xPath that means same xPath can be used for multiple element with different values.
Examples: In below examples I have just changed the text of the tab.
xPath for Kurser tab.
//a[contains(#class,'instrument-table-tab-menu') and text()='Kurser']
xPath for Historik tab.
//a[contains(#class,'instrument-table-tab-menu') and text()='Historik']
I would suggest you to practice with xPath more. Please go through this tutorial
Very easy to do if you have the class name with:
driver.findElement(By.className("PUT CLASSNAME OF ELEMENT HERE")).click();
You should give more precedence to css selector than xpath
CSS would be :
a[class^='instrument-table-tab-menu'][data-target$='2']
XPATH would be :
//a[contains(#class,'instrument-table-tab-menu') and contains(#data-target,'2')]
There are basically 4 ways to click in Selenium.
Using XPATH
Code trial 1 :
time.sleep(5)
driver.find_element_by_xpath("//a[contains(#class,'instrument-table-tab-menu') and contains(#data-target,'2')]").click()
Code trial 2 :
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(#class,'instrument-table-tab-menu') and contains(#data-target,'2')]"))).click()
Code trial 3 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[contains(#class,'instrument-table-tab-menu') and contains(#data-target,'2')]")
driver.execute_script("arguments[0].click();", button)
Code trial 4 :
time.sleep(5)
button = driver.find_element_by_xpath("//a[contains(#class,'instrument-table-tab-menu') and contains(#data-target,'2')]")
ActionChains(driver).move_to_element(button).click().perform()
Imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
I have an issue with located correct Xpath/ID input field, I am taking the following website as an example:
https://garden.lovetoknow.com/vegetable-garden/how-ripen-green-tomatoes-off-vine
After you open the link then scroll down a bit, you will see "write a comment" blue button, click it then fill the text and name, it will appear a reCaptcha example, I tried the following way to click the checkbox, but without success. I would really appreciate if someone can let me a hand on it
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(
"//iframe[starts-with(#name, 'a-') and starts-with(#src, 'https://www.google.com/recaptcha')]")));
new WebDriverWait(driver, 10)
.until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.recaptcha-checkbox-checkmark"))).click();
Seems you were close enough. The name of the <iframe> i.e. a-x2cp4vfbaqu8 looks dynamic, so it would be better to avoid the name attribute and you can use either of the following Locator Strategies:
Using css_selector:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("iframe[src^='https://www.google.com/recaptcha/api2/anchor']")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("span#recaptcha-anchor"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[starts-with(#src, 'https://www.google.com/recaptcha/api2/anchor')]")));
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.cssSelector("//span[#id='recaptcha-anchor']"))).click();
References
You can find a couple of relevant discussions in:
How to click on elements within an iframe to enable the captcha through the audio using Selenium and Python
Find the reCAPTCHA element and click on it — Python + Selenium
Dealing with reCAPTCHA in Python Selenium
I struggled with this for several days, it's actually very simple. The captcha is located in a frame, and in order to click on it, you need to switch to this frame.
I did it like this with Selenium:
driver.switchTo().frame(driver.findElement(By.xpath("//*[#title='reCAPTCHA']")));
driver.findElement(By.xpath("//span[#id='recaptcha-anchor']")).click();
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.
click here to check the page I am working on
The following is the java code:
driver.findElement(By.xpath(".//*[#id='ref_2665398031']/li[4]/a/span1")).click();
The element is actually in the left navigation pane.
Here when this particular statement is getting executed, I can see that browser is moving down, but it does not click that element.
You have not properly written the xpath:
".//*[#id='ref_2665398031']/li[4]/a/span1"
it should be : "//[#id='ref_2665398031']/li[4]/a/span[1]"
and if using WebDriver then
"//[#id=\"ref_2665398031\"]/li[4]/a/span[1]"
After correcting above if still error exists:
You are using a relative path which is relative to the element having id='ref_2665398031.
This id seems to be generated dynamically.
Please confirm two things:
Your code is not giving element not found error.
Page refresh is not changing this id. (If it is being generated dynamically than it will change on page refresh.)
Otherwise try using different approach:
driver.findElement(By.xpath("//*[contains(text(), '50% Off or more')]"));
Use this xpath:
driver.findElement(By.xpath("//ul[#id='ref_2665398031']/li[4]/a/span[1]")).click();
Try Below xpath it is working for me
driver.findElement(By.xpath("//ul[contains(#id,'ref_2665398031')]/li[4]/a")).click();
Try JavaScriptExecutor instead of using normal Selenium click method.
WebElement element = driver.findElement(By.xpath(".//span[contains(text(),'50% Off or more')]"));
((JavascriptExecutor) driver).executeScript("return arguments[0].click();", element);
I need some help trying to select a radio button on Selenium IDE.
This radio button value changes randomly, due this I cannot get the radio button selected.
There are multiple ways selenium can locate an element other than referencing the id of an element. Have you also tried using a xpath or css selector? Take a look at the selenium docs for locating an element for examples of using xpath or css selectors.
Post an example of your html and I can help you write the selector.
Use the following commands:
storeAttribute| path of area where radiobutton placed # id| variableName
Now you have dynamic id of that element . Use that value to click on it.
click| ${variableName}
Use a CSS or Xpath selector instead of the default used when you recorded your script.
For example, try this page with radio button examples, scroll down to the Milk/Butter/Cheese example:
The selector for the first radio button ("Milk") could be one of the following:
css = .table5 > input
css = .table5 > input[value='Milk']
A couple of ways you could select the second button would be:
css = .table5 > input ~ input
css = .table5 > input[value='Butter']
You may need to read up on CSS selectors and/or Xpath. You can do a web search for cheat sheets and primers. They are both essential for optimum Selenium scripting, be it with IDE, RC, or Webdriver.
You will find that Firepath is also a great tool to help you build your selectors.