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

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 ?
Attempt1
driver.find_element_by_class_name(".btn.btn-default").send_keys(os.getcwd() + "InputFiles/Error.png")
Error:
Attemp2:
driver.find_element_by_xpath("//div[#class='upload-btn__wrapper']").send_keys(os.getcwd() + "InputFiles/Error.png")
Error:

This xpath is supposed to work.
"//div[#class='upload-btn__wrapper']/button"
Hope this helps. Thanks.

Hope it help you. Let me know if you need further assistance.
driver.find_element_by_xpath("//div[#class='upload-btn__wrapper']")

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();

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.

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

Related

How to remove anchor tag '<a></a>' using javascript

How to remove anchor tag '' in java script?
When I inspected the page, below is the screenshot of what I got
Here is my code:
<div class="dropdownm1-content">
<b>SHOP ALL</b>
<b>SHOP BY CATEGORY</b>
<p class="mn_category">
Just get the Element by using the ID of it and then remove it with the remove() function. Like so:
var removeanchor = getElementById('YOURANCHORTAGID');
removeanchor.remove();
or without creating a variable:
getElementById('YOURANCHORTAGID').remove();
(replace YOURANCHORTAGID with the id of your anchortag). If you want to trigger this after an action just create a function and trigger it with the action you want :).
for further information check the mdn docs:
https://developer.mozilla.org/de/docs/Web/API/ChildNode/remove
You may should add some more information to your question for a more precise answer. However, for the time being this may helps you out.
If you try to use it, pay attention to the fact, that I only adressed the first Element with the class 'text_main' and only the first of its children with 'a' Tag. You may need to change this, according to your code.
// Removing a specified element without having to specify its parent node
container = document.getElementByClass("text_main")[0];
var node = document.getElementsByTagName("a")[0];
if (node.parentNode) {
node.parentNode.removeChild(node);
}
Further information:
https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild
For that specific link you showed:
document.querySelector('.dropdownm1-content .text-main a:first-child').remove()
Though I'd highly recommend curing the sickness, not the symptom.

Selenium---can't click on an image

I have this function and i am not able to click on all of the magnifying glasses from a page. I have tried until now, by using different alternatives. What is commented, is what i tried until now.
def lupa():
elements = browser.find_elements_by_css_selector("a[onclick='return Go(event, 2)'] > img[title='Details']")
for element in elements:
#element.click()
element.send_keys(Keys.SPACE)
time.sleep(1)
Please see below how looks the HTML code.
<a href="#" onclick="return Go(event, 2)">
<img title="Details" src="/common/images/Detail.gif">
</a>
This are the old XPATHs i used in order to click on the image.
#browser.find_element_by_xpath(".//*[#id='resultsTable']/tbody/tr[17]/td[11]/a[2]/img").click()
#lupa = browser.find_element(By.XPATH("//img[#src='/common/images/Detail.gif']"))
#lupa = browser.find_element(By.cssSelector("a[src='/common/images/Detail.gif']"))
#lupe = browser.find_elements_by_css_selector("a[src='/common/images/Detail.gif']"))
#lupa=browser.find_element_by_link_text("Details").click()
#lupa= browser.find_element_by_id("Details").click()
#elements = browser.find_elements_by_css_selector("a[src='/common/images/Detail.gif']"))
Thank you for your answer!
Cohen
Most of the locators you tried aren't valid. You probably should spend some time learning about the different locator types and how they work.
Given the HTML, the CSS selector below should find the IMG tag(s) you want.
img[title='Details']
Another thing, you are not using implicit wait correctly. It's set once for the life of the driver. Calling it over and over does nothing. You don't want to use implicit wait, use an explicit wait instead. Look at some tutorials for WebDriverWait.
EDIT: Clicking the IMG tag should work just find since it's surrounded by the desired A tag. But... if you need to click the A tag specifically, you should be able to use the locators below.
More specific CSS selector
a[onclick='return Go(event, 2)'] > img[title='Details']
XPath
//a[#onclick='return Go(event, 2)'][./img[title='Details']]

Unable to locate element for a website

Unable to inspect search label in www.walmart.com website. Any help much appreciated.
tried using css , id , etc but nothing helped. All the below code i tried but it didn't helped.
driver.findElement(By.id("global-search-input")).sendKeys("asifalikhan786#gmail.com");
driver.findElement(By.xpath("//*[#id='global-search-input']")).sendKeys("dssss");
driver.findElement(By.cssSelector("input[placeholder = 'Search']")).sendKeys("adahadha");
I looked at the page's code and noticed that By.id cannot work, because the object you want to access has no id.
I suggest you to try this:
WebElement element = driver.findElement(By.className("eader-GlobalSearch-input"));
element.clear();
element.sendKeys("your text");

Unable to Access DIV element using Watir

Hi I am trying to access the DIV element using watir but I am unable to do that,I have tried in different ways but couldn't access it,may be I think it need to be access through some parent element can anyone help me out?
My system Configurations
IE-8
Windows 7
I tried with the below command
#ie.div(:text,'COMPOSE').click
the command gets execute with no errors but no action is performed on the UI
The best solution appears to be switching to Watir-Webdriver. With Watir-Webdriver, #ie.div(:text,'COMPOSE').click will work as expected.
Assuming that is not an option, there are a couple of reasons why that same command does not work with Watir(-Classic) v1.6.7:
The first problem is that #ie.div(:text,'COMPOSE').click will find the first div that contains this text. This would be one of the ancestors of the div you want. As a result, Watir will send the click event against the wrong element.
The second problem is that the div is not responding to the onclick event fired by Watir. I am not sure why this problem exists.
To solve the first problem, you will need to be more specific when locating the div. In this case, the "role" attribute can be used since none of the ancestor elements have this attribute. Watir-Classic does not support using the role attribute as a locator. As a result, you will need to create a custom locator using an element collection and the find method:
#ie.divs.find{ |div| div.attribute_value('role') == 'button' && div.text == 'COMPOSE' }
To solve the second problem, it turns out that double clicking does work. While newer versions of Watir-Classic have a double_click method implemented, it does not exist in 1.6.7. You can replicate the method by calling the fire_event method:
.fire_event('ondblclick')
Putting it all together, the following will click the compose button:
#ie.divs.find{ |div| div.attribute_value('role') == 'button' && div.text == 'COMPOSE' }.fire_event('ondblclick')
There may be more than one element on the page with the text 'COMPOSE', some may be hidden. Try:
#ie.divs(:text,'COMPOSE').size
That is divs with an s.
Then you can try something like the following and see if you get a change in the UI:
#ie.divs(:text,'COMPOSE').each { |b| b.fire_event('click') }
I remember that fire_event works better, but would recommend consulting the docs for the difference between .click and fire_event.

Setting proper relative URL

If I am on the following page:
http://subsite.mysite.com/sites/department/class/SitePages/Home.aspx
and want to have a link to
http://subsite.mysite.com/sites/department/SitePages/Home.aspx
i.e. I want to keep /sitepages/home.aspx and move it one step up in the hierarchy
and not hard code http://subsite.mysite.com/sites/department/
Is this possible?
Thanks in advance.
Try:
My Link or
My Link