keepsmilinyaar
keepsmilinyaar

Reputation: 1007

Unable to locate element (without Id) in webpage using selenium

I've been trying to access/locate the element shown in the image and have tried various methods. xpath, classname, css but keep getting the error that the element cannot be found. Can you help please ?

Locating "Choose image"

Attempt1

driver.find_element_by_class_name(".btn.btn-default").send_keys(os.getcwd() + "InputFiles/Error.png")

Error:

enter image description here

Attemp2:

 driver.find_element_by_xpath("//div[@class='upload-btn__wrapper']").send_keys(os.getcwd() + "InputFiles/Error.png")

Error: enter image description here

Upvotes: 0

Views: 1113

Answers (5)

Aventador
Aventador

Reputation: 52

You can click on it with css selector also. Hope this will work for you. driver.findElement(By.cssSelector(".btn.btn-default")).click();

Upvotes: 0

anshul Gupta
anshul Gupta

Reputation: 1270

Try the below solution as well :

driver.findElement(By.xpath("//div[contains(@class,'margin-bottom')]")).findElement(By.xpath("//div[contains(@class,'upload-btn__wrapper')]")).click();

Explanation : I am navigating from the parent div which is "margin-bottom" div class and reaching out to the child div which we want to locate, which is "upload-btn__wrapper".

Let me know, if this works out.

Upvotes: 0

Rameshwar
Rameshwar

Reputation: 551

I suggest to use below Xpath as it will rely on your Text, so any changes in text of the button will result a failure of the test, which makes perfect sense.

//button[normalize-space(text())='Choose image']

Also use explicit wait before performing any operations with that element.

new WebDriverWait(driver, time).until(ExpectedConditions.visibilityOf(By.xpath("//button[normalize-space(text())='Choose image']")));
WebElement chooseImageButton=driver.findElement(By.xpath("//button[normalize-space(text())='Choose image']"));
chooseImageButton.click();

Upvotes: 0

Greedy
Greedy

Reputation: 11

Hope it help you. Let me know if you need further assistance.

driver.find_element_by_xpath("//div[@class='upload-btn__wrapper']")

Upvotes: 0

santhosh kumar
santhosh kumar

Reputation: 2019

This xpath is supposed to work.

"//div[@class='upload-btn__wrapper']/button"

Hope this helps. Thanks.

Upvotes: 1

Related Questions