so Ive been using selenium webdriver in Python3 right now, so far its working great and I can do most the things Id like, one thing though, on instagram, the xpath for the follow or following button is the same, Id assume it is for all web pages with buttons in the same place, but if the text and color are different when its follow vs following, is there a way to let python discern that?
Id like to write sort of like an if follow: do this, elif following: do this
the xpath for the button is this:
/html/body/span/section/main/article/header/div[2]/div[1]/span[2]/button
in html the text is different and all but I dont know how to make this work, if any of you have any advice thatd be great!
To "find" Following button you should use next xpath:
//button[#class="_jvpff _k2yal _988x3 _i46jh _nv5lf"]'
while for the Follow:
//button[#class="_jvpff _k2yal _csba8 _i46jh _nv5lf"]
Text of a button could be defined like this:
follow = driver._find_element_by_xpath('//button[#class="_jvpff _k2yal _csba8 _i46jh _nv5lf"]').text
Related
I'm new to using Selenium, and I am having trouble figuring out how to click through all iterations of a specific element. To clarify, I can't even get it to click through one as it's a dropdown but is defined as an element.
I am trying to scrape fanduel; when clicking on a specific game you are presented with a bunch of main title bets and in order to get the information I need to click the dropdowns to get to that information. There is also another drop down that states, "See More" which is a similar problem, but assuming this gets fixed I'm assuming I will be able to figure that out.
So far, I have tried to use:
find_element_by_class_name()
find_element_by_css_selector()
I have also used them in the sense of elements, and tried to loop through and click on each index of the list, but that did not work.
If there are any ideas, they would be much appreciated.
FYI: I am using beautiful soup to scrape the website for the information, I figured Selenium would be helpful making the information that isn't currently accessible, accessible.
This image shows the dropdowns that I am trying to access, in this case the dropdown 'Win Margin'. The HTML code is shown to the left of it.
This also shows that there are multiple dropdowns, varying in amount based off the game.
You can also try using action chains from selenium
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")
ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()
Source: here
I'm relatively new to using python & selenium. I'm trying to access NexisUni to automate a loop of searches. But, once I'm in NexisUni, I struggle to locate elements -- I get a "no such element" exception. I want to locate the search bar and input my search terms.
I've read about the fact that an iFrame might be present, and I need to switch frames. But, I don't see any frames! Is there a way to identify frames easily -- and could a frame be present without the word "frame" in the HTML? I've also tried loading the page longer and having the driver wait, to no avail.
The HTML code is below, the grey part is the piece I'd like to select:
HTML Code
The code I'm writing to identify it is:
SearchBar = driver.find_element_by_xpath('/html/body/main/div/div[13]/div[2]/div[1]/header/div[3]/section/span[2]/span/textarea').send_keys('search text')
I've also tried these two options:
find_element_by_class_name, find_element_by_id
WebDriverWait(driver,10).until(EC.presence_of_element_located)
... Any suggestions would be appreciated!
I struggle to select the Tinder like button. I just copied the Xpath from the web version of tinder. It works for the Dislike button not for the like button. I've also tried to add a sleep button in case the like button didnt render quickly enough.
def dislike(self):
dislike_btn = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/button[1]')
dislike_btn.click()
def like(self):
like_btn = self.driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/div/main/div/div[1]/div/div[2]/button[3]')
like_btn.click()
I've tried to select by CSS Selector but that didnt work either.
I am an absolute noob, so have mercy if I missed out on some relevant infos :D
try clicking the element directly without assigning it. This is working for me and also check if there are any overlapping elements on the like button.
driver.find_element_by_xpath('//*[#id="content"]/div/div[1]/div/main/div[1]/div/div/div[1]/div/div[2]/button[3]').click()
I'm trying to automate the logging in and unfollowing of people on the website Depop. I am having a bit of trouble clicking all of the 'unfollow' buttons. Each of the buttons have a different ID and Class and the only similarities between them is this HTML code:
<span> Following <span>
Is there any way to click every button with this HTMl code?
I have already tried finding the XPath of all the buttons but because there are so many of them, it would be difficult to find the XPath of every single button. I have also tried to find the class of the follow buttons but they are all different. There are no similarities in the css selectors.
I have tried doing something like this but to no success.
driver.get_attribute('<span> Following </span>').click()
For optimum results, the program would go through the page, unfollowing everyone that is currently being followed.
Here is the logic.
unfollows = driver.find_elements_by_xpath("//span[normalize-space(.)='Following']")
for btn in unfollows:
btn.click()
If you get staleElement exception with the above approach then you have to follow the following approach.
while len(driver.find_elements_by_xpath("//span[normalize-space(.)='Following']"))>0:
btn = driver.find_element_by_xpath("(//span[normalize-space(.)='Following'])[1]")
btn.location_once_scrolled_into_view
driver.execute_script("arguments[0].click();",btn)
I am attempting to scrape the Census website for ACS data. I have scripted the whole processes using Selenium except the very last click. I am using Python. I need to click a download button that is in a window that pops when the data is zipped and ready, but I can't seem to identify this button. It also seems that the button might change names based on when it was last run, for example, yui-gen2, yui-gen3, etc so I am thinking I might need to account for this someone. Although I normally only see yui-gen2.
Also, the tag seems to be in a "span" which might be adding to my difficulty honing in on the button I need to click.
Please help if you can shed any light on this for me.
code snippet:
#Refine search results to get tables
driver.find_element_by_id("prodautocomplete").send_keys("S0101")
time.sleep(2)
driver.find_element_by_id("prodsubmit").click()
driver.implicitly_wait(100)
time.sleep(2)
driver.find_element_by_id("check_all_btn_above").click()
driver.implicitly_wait(100)
time.sleep(2)
driver.find_element_by_id("dnld_btn_above").click()
driver.implicitly_wait(100)
driver.find_element_by_id("yui-gen0-button").click()
time.sleep(10)
driver.implicitly_wait(100)
driver.find_element_by_id("yui-gen2-button").click()
enter image description here
enter image description here
Instead of using the element id, which as you pointed out varies, you can use XPath as Nogoseke mentioned or CSS Selector. Be careful to not make the XPath/selector too specific or reliant on changing values, in this case the element id. Rather than using the id in XPath, try expressing the XPath in terms of the DOM structure (tags):
//*/div/div/div/span/span/span/button[contains(text(),'Download')]
TIL you can validate your XPath by using the search function, rather than by running it in Selenium. I right-clicked the webpage, "inspect element", ctrl+f, and typed in the above XPath to validate that it is the Download button.
For posterity, if the above XPath is too specific, i.e. it is reliant on too many levels of the DOM structure, you can do something shorter, like
//*button[contains(text(),'Download')]
although, this may not be specific enough and may require an additional field, since there may be multiple buttons on the page with the 'Download' text.
Given the HTML you provided, you should be able to use
driver.find_element_by_id("yui-gen2-button")
I know you said you tried it but you didn't say if it works at all or what error message you are getting. If that never works, you likely have an IFRAME that you need to switch to.
If it works sometimes but not consistently due to changing ID, you can use something like
driver.find_element_by_xpath("//button[.='Download']")
On the code inspection view on Chrome you can right click on the item you want to find and copy the xpath. You can they find your element by xpath on Selenium.