I am trying to click an element with Selenium, that has a specific title attribute. I have tired do use an xpath before, however, the problem is that there are two buttons on the website with the same xpath. If one button is active, it has the same xpath as the other when its active and vice versa.
The only thing that differentiates these two buttons in the title attribute.
<a class="qPKfxd" href="SOME LINK" title="List">
Basically I am trying to only click that element if the title is "List".
Has anyone got an idea of how to specify that with Selenium?
Please let me know if you need to view more code.
You can locate an element by attribute.
xpath:
//a[#title="List"]
css_selector
[title="List"]
Related
I'm trying to use webscraping (via Python and Selenium) to create a worksheet with companies of interest to my boss. Most of it is working, I just can't seem to get hold of the "Next Page" button. Relative and absolute XPaths, CSS selectors, nothing seems to work, since every time you generate/switch pages they're diferent. (The relative XPath usually is '//*[#id="ember{SOME RANDOM NUMBER}"]') What could I do? There are other buttons with the same relative XPath structure in the page.
The Next page button has the same XPath for all the pages.
It is //button[#aria-label="Next"]
You should locate this element according to the aria-label attribute, not the id attribute value.
You can use class_name function to locate 'Next' element
next_button = wd.find_element_by_class_name('artdeco-pagination__button next').click()
I'm new to Python Selenium and trying to select the ui-button tag in the following DOM structure:
...
<div>
<ui-button>
<button type='submit'>TEST</button>
</ui-button>
</div>
There's a lot of HTML in this document which I havent included, so I understand that an efficient XPath statement can accomplish this but don't know how to do it.
There's multiple ui-button tags in the page, but only one with a child button type='submit', and the goal is to click on the ui-button tag with selenium. There's no other easy way to identify these tags. So what I was doing was selecting the submit button with a css selector then using XPath to go up 1 parent, but I need to be able to select the ui-button with a single statement.
This is my best attempt:
"./ui-button/input[#type='submit']"
I'm trying to select the ui-button with a child input button that is of type 'submit'.
Thanks in advance
To select the parent ui-button node according to it's child with type='submit' you can use any of the following XPaths:
//ui-button[./button[#type='submit']]
or
//button[#type='submit']/..
or
//button[#type='submit']/../..//ui-button
But all this seems strange to me sense normally you have to click the button element, not element with strange tag name like ui-button.
However if <button type='submit'>TEST</button> is not the immediate child of the ui-button element you can use only one option from the 3 mentioned above with a slight change:
//ui-button[.//button[#type='submit']]
this means: an element with ui-button tag name somewhere on the page having somewhere inside it an element matching //button[#type='submit'] locator
In order to get the full XPath you can go to chrome and simply right-click -> Inspect to go to the chrome developer tools. Then you can right-click on the element in the HTML, go to copy, and click Copy Full XPath. Then you can use find_element_by_xpath(XPath goes here) to find the element.
I am trying to save the same stuff with selenium in my Yandex account, the problem is that when I try to pass the code to click the button "save to Yandex disk", selenium pass me the message unable to locate the element.
Thats my code:
browser.find_element_by_xpath('/html/body/div[1]/div/div[2]/div[1]/div[1]/div/div[1]/div[3]/button[1]').click()
this is the page with the button "Save to yandex disk": https://yadi.sk/d/0ReZErv_cLl1-w
I read that u can also pass elements by name or by CSS selector, but when I try with firefox inspector to copy element, the browser gives me strange code.
Any suggestions?
..of course, the same error with or without logged into Yandex.
Thank you
You can use this XPath for detecting needed element:
//div[#class = 'folder-content content content_other content_dir']//button[contains(#class, "save")]
Use this xpath //*[contains(text(),'Save to Yandex.Disk')] to click "Save to Yandex.Disk" button
First thing is that we need to improve the xpath you are using to find the element we should use the xpath that is relative but in your case you are using the xpath that is absolute, I use the extension chropath in chrome to find the xpath of the element
Below mentioned is the chro path I would recommend you to use although the above two mentioned xpath can also be used
Let me know If you have any more queries, I can form a good xpath for you
//div[#class='folder-content__header']//span[contains(text(),'Save to Yandex.Disk')]
Below mentioned is one of the xpath I have used in my own project take a look maybe this can improve your horizon
//label[contains(text(),'Plant Code*')]//parent::div[#class='rb_Work_FieldContainer']//following-sibling::div[contains(#class,'rb_Work_FieldValueArea rb_Work_FieldValueArea_create ')]//textarea[#class='textarea']
I am working with python and selenium to click on the Photo/Video button on a facebook page. The HTML associated with this seems to have a list item (li) inside a ui. The html is as in the following image. The button circles is the one I am trying to press.
Can anyone please tell me how should I press the Photo/Video button?
Can you try this code?
I used the xPath method and contains() to compare the text in the div.
By the way, the found object does not have the click related function, and the click function seems to be a tag among its parents
The syntax for finding a parent in xPath is /.. and I used this
https://stackoverflow.com/a/3655588/12582501
driver.find_element_by_xpath('//div[contains(text(),"Photo/Video")]/../../../a').click()
Facebook has an intresting thing: testids
With this IDs you can click all of clickable elements on the site
driver.find_element_by_xpath('//div[#data-testid="photo-video-button"]').click()
In this case you can exec your code, when on page will be another element with text "Photo/Video"
I am trying to click a button to show more comments on a discusson thread on http://disqus.com/
The HTML looks like following:
<div class="load-more" data-role="more" style="">
Show more
</div>
I have tried using the xPath from the button like following:
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver")
driver.get(url)
driver.find_element_by_xpath('''//*[#id="posts"]/div[3]/a''').click()
But I get the NoSuchElement exception.
What is the proper way of clicking a button with that type of HTML?
UPDATE: Worked when I switched to a specific iFrame:
driver.switch_to.frame('myID')
Then loaded by class name:
element = driver.find_element_by_class_name('load-more')
element.click()
NOTE: That the click() did not work when it was performed on the same line like driver.find_element_by_class_name('load-more').click()
Xpath should be a last resort for finding elements. For your case, I would try
driver.find_element_by_class_name("load-more__button"));
Using btn load-more__button won't work as compound classNames aren't valid.
If that still doesn't work, Id recommend using css selectors before Xpath.
However, if none of these options work, and your Xpath is still not working, you should use the firebug add on for Firefox to ensure the Xpath you are using is correct.
Here's some of my other answers that may help you get started with Firebug:
https://stackoverflow.com/a/38980488/3537915
https://stackoverflow.com/a/38723782/3537915
https://stackoverflow.com/a/38744577/3537915