I am trying to click the search button in this link here
I would like to click the search button and then download all URL's on the next page but currently it is finding monthly list
The link below takes you to a screenshot of my code where it outputs the monthly list button instead of searcj/
Python code selenium
Welcome to Stack Overflow!
You can use the .click() method to click an element object, and the .find_element_by_xpath() method to find the element. I've located that the element's full XPATH is /html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2].
You can implement all the pieces together like so:
driver.find_element_by_xpath("/html/body/div/div/div[3]/div[3]/div/form/fieldset/div[5]/input[2]").click()
if you are starting I recommend you using some extension to help you find the xPath of the elements.
I used Xpath Helper from Chrome, but you can use any other.
The Xpath of the Search button is:
/html[#class='js']/body/div[#id='idox']/div[#id='pa']/div[#class='container'][2]/div[#class='content']/div[#class='tabcontainer']/form[#id='weeklyListForm']/fieldset/div[#class='buttons']/input[#class='button primary']
After you have the XPath you can use it in the selenium driver to find the elements.
This can be enough, but I recommend you to learn in depth how this works to know exactly what it is doing.
Related
I am making an automation for download data from a weather institution.
The issue is that in my effort to make it more independent I am trying to make Selenium to Tab keys to a certain spot, so the Browser focus can "Walk" to the download button. When I call the click() function it doesn't do anything. So I tried to Extract the XPath with the function get_attribute("xpath") but it returns None. How I can extract the XPath?
I am going to paste the issue down here:
Bandera=driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input')
Bandera.click()
Bandera.click()
## So Here i just select and dis-select a checkbox just to be near the Download button.
actions = ActionChains(driver)
actions.send_keys(Keys.TAB * 1 )
actions.perform()
#Here i just tabed to the button
Accion=driver.switch_to.active_element
#Maybe, here is when i lost the focus of the button?
Descarga_Actual=Accion.get_attribute("xpath")
Thank you and sorry to borrow your time.
To make a click on hover I would use the following sequence:
your_dropdown_locator.click()
dropdown_option = driver.find_element_by_xpath("dropdown option locator")
actions = ActionChains(driver)
actions.move_to_element(dropdown_option)
actions.click().perform()
But, this is the approach that is usually used for dropdowns.
You use: driver.find_element_by_xpath('/html/body/div[2]/div[2]/div/div[3]/div/div/div/div[1]/div/div/div[2]/div/table/tbody/tr[1]/td[1]/div/input') This is the main problem of your code.
If you pasted html code of this dropdown (or button you need to click), people would help you to find a better locator. XPath/CSS must be unique. In your case the locator is very bad.
Also, I see no sense making Bandera.click() two times.
In your case, as I understand, you just need to click the button. So the locator is your main problem.
You need to find the correct locator, to wait till the button is clickable and then to click it.
Another problem in what you are trying to do:
get_attribute("xpath") looks like incorrect expectation of how get_attribute function works. Check at least here what this function means Python Selenium: Find object attributes using xpath
Inspect Youtube Page Element
I am new to Python and I am learning how to automate webpages. I under the basics around using the different locators under the inspect element tab to drive my code.
I have written some basic code to skip youtube ads however I am stuck on finding the correct page element to agree to the privacy policy pop up box in Youtube. I have used ChroPath to try and find the xpath of the page however there doesn't appear to be one. I was unable to locate any other page elements and I was wondering if anyone has any ideas on how I can automate the click of the 'I Agree' button?
Python Code:
from msedge.selenium_tools import Edge, EdgeOptions
options = EdgeOptions()
options.use_chromium = True
driver = Edge(options=options)
driver.get('http://www.youtube.com')
def agree():
while True:
try:
driver.find_element_by_xpath('/html/body/ytd-app/ytd-popup-container/paper-dialog/yt-upsell-dialog-renderer/div/div[3]/div[1]/yt-button-renderer/a/paper-button').click()
driver.find_elements_by_xpath('.<span class="RveJvd snByac">I agree</span>').click()
except:
continue
if __name__ == '__main__':
agree()
Youtube Inspect Element Screeshot is below:
I don't know if the xpath in your code is right as I can't see the whole html structure of the page. But you can use F12 dev tools in Edge to find the xpath and to check if the xpath you find is right:
Open the page you want to automate and open F12 dev tools in Edge.
Use Ctrl+Shift+C and click the element you want to locate and find the html code of the element.
Right click the html code and select Copy -> Copy XPath.
Then you can try to use the xpath you copy.
Besides, find_elements_by_xpath(xpath) will return a list with elements if any was found. I think you need to specify which one element of the list to click with. You need to pass in the value number of the elements list like this [x], for example:
driver.find_elements_by_xpath('.<span class="RveJvd snByac">I agree</span>')[0].click()
When inspecting the page elements I overlooked the element of iframe. After doing some digging I came across the fact I had to tell the Selenium Driver to switch from the main page to the iframe. I added the following code and now the click to the 'I Agree' button is automated:
frame_element = driver.find_element_by_id('iframe')
driver.switch_to.frame(frame_element)
agree2 = driver.find_element_by_xpath("/html/body/div/c-wiz/div[2]/div/div/div/div/div[2]/form/div/span/span").click()
driver.switch_to.default_content()
I need to find the webelements like id="rcmrowgeneral".
A standard driver.find_element_by_id() is not working.
It's like they are inside another HTML page.
How can I find them with selenium to interact with them?
You can do this by following the selenium syntax which states if you want to find a specific tag inside an iframe you have to switch to that iframe first and then you can use selenium query to find it. Since you've not posted any code over here i assume you've background knowledge of handling automation processes. You can do this task by following this naming convention:
driver.switchTo().frame("id or name of the element")
driver.find_element_by_id("your id here")
Your element is inside of an iframe. First you need to switch to the iframe and then it is a good practice to wait for the element to be visible/clickable before interacting with it.
driver.switch_to().frame(driver.find_element_by_id("TiscaliWebmailFrame"))
WebDriverWait(driver, 20).until(expected_conditions.presence_of_element_located((By.ID, "rcmrowgeneral"))).click()
I need to click the "next" button on the following page:
https://www.amazon.com/gp/goldbox/ref=gbps_ftr_s-4_bedf_wht_29726380?gb_f_deals1=dealStates:AVAILABLE%252CWAITLIST%252CWAITLISTFULL%252CEXPIRED%252CSOLDOUT%252CUPCOMING,includedAccessTypes:,sortOrder:BY_SCORE,enforcedCategories:2972638011&pf_rd_p=afc45143-5c9c-4b30-8d5c-d838e760bedf&pf_rd_s=slot-4&pf_rd_t=701&pf_rd_i=gb_main&pf_rd_m=ATVPDKIKX0DER&pf_rd_r=60XTK6YDM9WEAFXB6519&ie=UTF8
I have the following code that will not find xpath element for the "next button":
browser.find_element_by_xpath("//ul[#class='a-pagination']/li[#class='a-last']/a").click() # Click on next button
I have also tried this variation:
browser.find_element_by_xpath("//div[#id='pagination-next-6885645543374035']/ul/li[#class='a-last']/a")
I also tried directly copying the xpath from the inspector. Neither of these three options work.
Thanks in advance for your help.
The issue is Amazon attaches a numerical ending onto the ID that is different each time the page loads. Finally found a way to select it using xpath:
browser.find_element_by_xpath("//span[#class='a-declarative']/div[2]/ul/li[#class='a-last']/a")
There are two dropdown element code: one is standard option-select and the other is made of div, ul, li elements.
And somehow both are used to select a dropdown element via javascript...
Problem is selenium is not able to click the element and throws not visible exception.....
See the dropdown box here: [Its below "Top 5" tab]
http://www.oddsbox.com/baseball/mlb/record/section.odd
Following solutions don't help either:
Python Selenium: Find object attributes using xpath
selecting element in python selenium
Selenium nested li div menu select() or click() python
how to select custom dropdown list element from selenium
It would be nice if you'd post your code, so we can see a bit clearer what's happening.
Also admitted, I did not check all of your links to see everything that doesn't work. However my guess is this:
If you get an ElementNotVisible exception, then you should probably make your element visible before selecting it.
In this case I'd forget about the selecting commands and all and just :
- click on the element to open and reveal the menu and then
- click on the desired element inside that list.
Looks something like :
driver.find_element_by_xpath(".//*[#id='ctmSelectBox4_wrap']/button").click()
driver.find_element_by_xpath(".//*[#id='ctmSelectBox4_wrap']/div/ol/li[6]/label/span").click()
I personally detest these ugly xpaths (especially for maintainability), and probably would change that somehow, but that's not the scope of this question.
Hope that helps!