Webdriver and push button - selenium-webdriver

i try to make in webdriver a test.
go to http://programyedukacyjne.pl/konkurs/galeria-prac.html
click for example first vote "GŁOSUJ [star]"
and should appear a box to put email.
But nothing happend just add to url in browser "#vote_box_22"
I tryed use click(), Action and javascript click but still there no appear a form...
here is begine:
driver.get("http://programyedukacyjne.pl/konkurs/galeria-prac.html");
WebElement location = driver.findElement(By.name("filter_miejscowosc"));
location.sendKeys("nowy sącz", Keys.ENTER);
here is where I have problem
WebElement voteBtn = driver.findElement(By.xpath("html/body/div[4]/div/div[3]/div/div[4]/div/div[1]/div[2]/div[2]/div[1]/a"));
voteBtn.click();
my secound solution
new Actions(driver).moveToElement(voteBtn).click().perform();

Try using a more specific selector that pinpoints the link more accurately in a less brittle way. The following CSS selector should select the vote link for the first result shown in the list:
WebElement voteBtn = driver.findElement(By.cssSelector("div.praca:nth-of-type(1) a[href*='vote_box']"));
voteBtn.click();

Related

How to locate the XPATH of the Continue button on LinkedIn?

I right clicked the button, selected "Inspect" command and copied and used both XPATH and full XPATH. Neither of them worked for any of the commands below.
xpath = '//*[#id="ember949"]/footer/button[1]'
xpath_full = '/html/body/div[3]/div/div[2]/artdeco-tabs/artdeco-tabpanel[2]/form/footer/button[1]'
button = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, xpath)))
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, xpath_full)))
Can someone please explain? Thank you.
Without the HTML, I cannot give a definitive answer.
Looks like the first id may be dynamic and could change.
As for the second one, the full XPaths are brittle and I do not use them at all.
There could be an iframe that you should switch to first.
You could try searching the entire page for a button element with the continue-btn class:
//button[#class='continue-btn']
I would recommend using the Selector / CSS Selector. In the code just change
By.XPATH
to:
By.CSS_SELECTOR
and instead of copying the XPATH copy the Selector or in some cases CSS Selector.

Web Crawlling A Site made in AngularJS

https://mahabhulekh.maharashtra.gov.in/ click on the link to see the site
I want to download a file from a site made in Angularjs. On first page there are 6 links which I have to crawl one by one. Selecting any link will lead to a page having 3 Dropdown.
Select District from dropdown which lead to related talukas in second dropdown.
Select Taluka from dropdown which lead to related Villages in third dropdown.
Select Village.
Now there is radio buttons from which I have to select the one which has survey number.
Now have to insert some digits and Click on Button. Now on the same page a dropdown will be shown from which I have to select all the options one by one and Click on button, the related file will get downloaded in pop up, so have to allow popups.
Code I have done is in selenium. I have just Clicked on the link on first page and traversed to second but now have to select dropdown but dont know how to do this as this site used Angularjs
System.setProperty("webdriver.chrome.driver","D:\\chromedriver1.exe");
WebDriver driver = new ChromeDriver();
final NgWebDriver ngDriver;
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
String baseUrl = "https://mahabhulekh.maharashtra.gov.in/";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
driver.findElement(By.partialLinkText("अमरावती")).click();
There's really nothing special about this being built with AngularJS. You don't show what you've tried after clicking on the link from the first page, so i'm guessing that you just need some guidance of what to do next.
If you inspect the new page, you'll see that there is a "select" webElement that you need to interact with. (be sure to use WebDriverWait to wait for the control to be available). There is a convenience class for interacting with these elements:
from selenium.webdriver.support.ui import Select
Here's the first dropdown:
dist_select = Select(driver.find_element_by_id('distSelect'))
select.select_by_value('string:5')

How do i select an element from a google drop down in selenium?

I have a field where it picks the location from a dropdown which is from google dropdown and not a native dropdown. So how can i click on that element in dropdown using selenium.
The results are show in an HTML div. The only reason why you are not able to find this div in HTML is because it appears somewhere near the bottom of the HTML and it is fragile enough to get dismissed with a click anywhere.
Coming to the code, your search results are generated inside individual divs with id="pac-item". So you have to just find them and click which ever item you want. Please don't forget to mark the answer
WebDriver driver = new ChromeDriver();
driver.get("http://www.nobroker.in/");
driver.findElement(By.id("locationGoogle")).sendKeys("n");
Thread.sleep(4000);
List<WebElement> elements = driver.findElements(By.className("pac-item"));
for(WebElement element : elements)
{
System.out.println(element.getAttribute("innerHTML"));
if(element.getAttribute("innerHTML").contains("Sopara"))
{
element.click();
}
}
Of course you will update the code to make it more efficient.

once selenium web driver clicks any button, then it wont click any other button further

I had a problem with the web driver while page get refresh
scenario:
click on the 1st button and the page got refresh. Once the page refresh, then the driver cant able to find the position of the second button. the second button is save function so that I cant able to save that page.
The error which I got while running :
unknown error: Element is not clickable at point (257, 898).
Other element would receive the click:
<div id="divModel" class="dialog" style="display: block; height: 2037px; width: 1450px;">...</div>
1) is there any solution to refresh the variables on the page without refreshing the whole page?
2) is there any solution to find out the next button position after clicking the first one on that page?
First of all, as we do not have any clear idea what happens with you. First thing checks that is your Xpath is changing after refreshing the page. another thing is may be your page need some wait.
Try below code:-
WebElement element= driver.findElement(By.xpath("YOUR XPATH OR ANY LOCATOR"));
WebDriverWait wait = new WebDriverWait(driver, 30);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated((By) element));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
At last if nothing work then try to use Thread.sleep(5000); .
It's not a good practice even it is not recommended to use Thread.sleep but you can at least try once to ensure that the problem is related to wait only
It's not a solution, it's workaround: if you use Chrome driver then use FF/IE driver instead. I encountered similar problem with Chrome driver not so long ago, turned out to be Chrome driver bug.
Also, if you use PO model, you can use #CacheLookup annotation:
#CacheLookup
#FindBy(id = "your_id")
private WebElement saveButton;
Marker annotation to be applied to WebElements to indicate that it
never changes (that is, that the same instance in the DOM will always
be used)

Cannot find button element on form using Webdriver

I'm having trouble trying to click on on a button within a form.
I've tried xpath, cssselector, className, id, but still cannot find it.
Here's the HTML snippet for the button:
<input type="button" value="Continue" id="ni-reg-btn-register" class="btnNext ni-reg-btn-register">
I'm using WebDriver in Java
Getting this trace:
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
Command duration or timeout: 30.09 seconds
Frequency: 100%
Browser: Firefox
URL: https://subscription.thetimes.co.uk/webjourney/webj_capturecustomerdetails
I've tried each of the following lines of code one by one (but no luck):
driver.findElement(By.className("btnNext ni-reg-btn-register")).click();
driver.findElement(By.cssSelector("buttons#ni-reg-btn-register.btnNext ni-reg-btn-register")).click();
List<WebElement> buttonlist= driver.findElements(By.className("btnNext ni-reg-btn-register"));
driver.findElement(By.id("ni-reg-btn-register")).click();
driver.findElement(By.xpath("//*[#id="ni-reg-btn-register"]")).click();
I was able to click the Continue Button using the below xpath.
//input[#id='ni-reg-btn-register']
I would suggest trying a WebDriverWait call to wait for the element in question to exist on the page prior to interacting with it. The Java documentation can be found here. It appears that while you are using the correct locator and your page is working, there is a timing issue where Selenium is trying to access the element prior to it being loaded.
EDIT: I was unable to locate an element on the provided page with the supplied id. I assume I am missing a step, but I did a search on the HTML for that page and found nothing.
I dont find the above button on the page link you provided ..
better chk the page link and try to give some wait statement after page load ...
implicit wait would be better or simpli Thread.sleep(2000);

Resources