I am using selenium to automate repetitive task. It works really well. But I ran into a issue where I get an email sent to me which has a download button in it. I am not sure how to click on it? I can get the xpath but that only works once. Any help would be great!
Here is the button I am trying to select
Email's button
download_button = driver.find_element_by_xpath('//*[#id="m_-2340741114430056650background-table"]/tbody/tr/td/table/tbody/tr[4]/td/table/tbody/tr/td[2]/table/tbody')
The xpath only works once after I grab that specific path. The idea is that this email is sent every week and for me to automatically select the button for the download. There isn't other identifiers like name or id, etc for this element. The email is sent from another company, so I don't think I have access to changing the way it is sent.
Please let me know if you need more information. Thank you for any help.
Related
I'm trying to scrape my Instagram DMs (direct messages). However, I don't know how to automate scrolling down the column to get the additional usernames that messaged me.
I've tried this, as well as the scrolling code listed here.
However, neither work because Instagram's DM page is broken into columns. The full page itself does not need to be scrolled. Just the first column. For reference, here's a screenshot of what the Instagram DM page looks like. I blocked out my username and the usernames of people who DMed me for privacy reasons
Does anyone have any ideas on how I can modify the script that scrolls down the page to accommodate the column size?
Thank you for taking the time to read my question and help in any way you can.
You should instead try to use the export data feature on Instagram, you will get the JSON files of all the messages and media you have sent or received. This will make your task a lot easier.
https://help.instagram.com/contact/505535973176353
https://www.instagram.com/download/request/
Instead of targeting the window you should target the messages box element to scroll:
scroll_y = 1000
driver.execute_script(f"document.getElementsByClassName('N9abW')[0].scrollTo(0,{scroll_y})")
if it doesn't work with class name try with Xpath:
path = "//*[#id='react-root']/section/div/div[2]/div/div/div[1]/div[2]/div/div/div"
script = f"document.evaluate({path}, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.scrollTo(0,{scroll_y});"
driver.execute_script(script)
Adjust scroll if needed
I am trying to code a program for somebody, that is possible to send and delete messages at the same time in the browser Discord application.(I think he wants to use this to spam?) He told me to write the code in Python, so I used python with Selenium. At this point I got everything working so far, the login works, the channel select works if you just insert the channel link directly. It also managed to let it send some messages. But problem now is, that I don't know how to delete the message after it was send, beacuse you need to click a "More" button first. But this more Button
I would use Webbot for that:
from webbot import Browser
web = Browser()
web.click('More')
that module has quite a few useful simple functions so i encourage you to try it
I try to login a site automatically by selenium.
My script sometimes works smoothly but sometimes it does not send full-text of user name or passwork to relavant inputs so it then failed to login.
I try to insert implicitly_wait but it appears not to solve this problem totally.
I wonder if there is any function to require the webdriver to fullfill each of input before excecute another one in selenium.
Here is may attempt:
browserdriver.get(url)
#fill username
browserdriver.implicitly_wait(25)
psusername=browserdriver.find_element_by_xpath("//*[#class='form-group'][1]/input[#name='username']")
psusername.click()
psusername.send_keys("1234567890")
#fill password
browserdriver.implicitly_wait(25)
pspass=browserdriver.find_element_by_xpath("//*[#class='form-group'][2]/input[#name='password']")
pspass.click()
pspass.send_keys("abcdefgfk12345")
browserdriver.implicitly_wait(25)
pssubmit=browserdriver.find_element_by_xpath("//*[#class='login-btn']/button[#type='submit']")
pssubmit.click()
Pls, explain for me why I get stuck on this problem and how to solve it! thanks alot
EDIT1: I add the target site address: click here You should click on login words (Đăng nhập) on the top right screen to see the popup login pannel.
EDIT 2: Thank to guide of below comments, I try to add some code lines to force it to key individual letter and it appears to work well so I add solution here:
text_input="1234567890"
for i in text_input:
psusername.send_keys(i)
Use set attribute instead of sendkeys
here is the example code:
webdriver.executeScript("document.getElementById('elementID').setAttribute('value', '1234567890')");
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.
I'm trying to write a program in python with which I open gmail, log in, and send an email from my account. The only problem is I can't find the name or id of any of the buttons I need to have either a name or id for. Can anyone help me with this? I'm sorry if this question is considered too simplistic but I have been looking all through the code I can see and I can't seem to find it. I can seem to do everything I need to do for this program except this. Thank you.
Edit: I have been using Selenium. Basically how the program will work is it opens my browser, goes to gmail, inputs my email in the appropriate box, clicks the button to move on, inputs my password in the appropriate box, and clicks the button to sign in. Then it clicks the "compose email" button and inputs text then clicks the "send" button. I've already basically made it work apart from the buttons which is the issue.
Edit:
def openGmail():
from selenium import webdriver
import time
driver = webdriver.Chrome('/Users/elliot/Desktop/chromedriver')
driver.get('http://www.gmail.com');
signinbox = driver.find_element_by_name('identifier')
signinbox.send_keys('myemail#gmail.com')
signinbutton = driver.find_element_by_id('identifierNext')
signinbutton.click()
time.sleep(1)
passwordbox = driver.find_element_by_name('password')
passwordbox.send_keys('mypassword')
passwordbutton = driver.find_element_by_id('passwordNext')
passwordbutton.click()
in google or secure website similar to that, they don't use buttons basically
so what you can do is use css selectors in selenium
example
WebElement nxtBtn = driver.findElement(By.cssSelector("*[text='Next']"));
or
WebElement nxtBtn = driver.findElement(By.cssSelector("*[innertext='Next']"));