Hello good morning/evening! i want to ask, is there a way to differentiate this two type of way telling that those channels does not exist? I'm creating a tool to find existing channel on telegram. Here are the examples.
When i try to run my code, the channels that does not exist still counts as an existing channel.
Here is the code that use, to differentiate this 2 type of way telegram telling that the channel does not exist. I used the help of selenium and chrome driver to find the existing telegram channel.
for ss in cli1:
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=chrome_options)
driver.maximize_window()
driver.implicitly_wait(10)
driver.get(f"https://t.me/{ss}")
try:
if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div'))):
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
elif WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.XPATH, '/html/body/div[2]/div[2]/div/div[2]/a'))):
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
except:
print(f'URL https://t.me/{ss} is valid')
exists.append(ss)
driver.quit()
sleep(5)
Is there a way for a telegram channels that does not exist still say no available, instead of making the site available but it doesn't have any channel at all.Here are the link examples. Non existing site 1 https://t.me/minecraft Existing Site https://t.me/minecraftapkdownload Non Existing site 2https://telegram.org/
Thanks for the help!
In your code, in the try block, make the below changes
try:
if WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSSSELECTOR, 'tgme_action_button_new shine'))):
print(f'URL https://t.me/{ss} is valid')
exists.append(ss)
except:
print(f'URL https://t.me/{ss} is not available')
Not_Exist.append(ss)
I would recommend you to use path made up of classnames/ids etc.
Paths like/html/body/div[2]/div[2]/div/div/div[1]/div[2]/a/div are prone to throw errors even with minimal changes to the GUI
In the above code, I have assumed that the View in Telegram button is shown if a channel is valid and for all other pages no such button is shown.
The class for the button is 'tgme_action_button_new shine'
The code checks if this button exists, if yes then it adds the page to the valid list, else to the invalid list
Related
hi i try to write a bot that automates once a day. Logged into my LinkedIn chat with a person who sends me posts and every message i wiil have post and bot need to to respond my email and write send my you cv something similar to this.
def start():
driver.get("https://www.linkedin.com")
time.sleep(3)
username = driver.find_element(By.ID, 'session_key')
username.send_keys(MyUsername)
time.sleep(0.7)
password = driver.find_element(By.ID, "session_password")
password.send_keys(MyPassword)
time.sleep(0.3)
button_sign = driver.find_element(By.XPATH, '//*[#type="submit"]')
button_sign.click()
time.sleep(3)
# scroll_down_random()
button_messaging = driver.find_element(By.XPATH, '//*[#id="global-nav"]/div/nav/ul/li[4]/a')
button_messaging.click()
time.sleep(2)
search_chat = driver.find_element(By.XPATH, '//*[#id="search-conversations"]')
search_chat.send_keys(name_of_chat)
search_chat.send_keys(Keys.ENTER)
time.sleep(2)
submit_application = driver.find_element(By.ID,'//*[#id="ember65"]')
submit_application.click()
but I am stuck on two things one After I search I can't select the first chat and I Run in a loop between messages in posts.
after
submit_application = driver.find_element(By.ID,'//*[#id="ember65"]')
submit_application.click()
need to click the first person after I search. and how I can sty always connected if I upload it to the cloud without having to connect every time
I think the ID you are searching for is incomplete, I get an ID like
"overlay-conversation-card-ember133"
This might be different for different devices so I believe you should use XPATH here rather than the ID.
Hope this helps. 👍🏻
EDIT:
It seems selenium .click() does not always register, use .submit() although this doesn't work for all browsers incase of that use .send_keys(Keys.ENTER)
I have this webpage https://account.proton.me/login?language=en where I am trying to switch to modal after logging in to the page. Please note that the modal appears even if you give wrong id and password, so to reproduce you can use the same code below
driver.get('https://account.proton.me/login?language=en')
usernameField = driver.find_element(By.XPATH,'//*[#id="username"]')
usernameField.send_keys("kuchbhirandom#some.thing")
passwordField = driver.find_element(By.XPATH,'//*[#id="password"]')
passwordField.send_keys("yehbhikuchbhi")
loginbutton = driver.find_element(By.XPATH,'//button[#type="submit"]')
loginbutton.click()
The above code gives us the modal
I ttried checking the window handles and switching to them one by one which gives me
driver.window_handles
['CDwindow-34B695696D2295F87F84F06321D10117', 'CDwindow-212C47AEC0CCD8240A4F9675D5B5BEF2', 'CDwindow-5A39DFE9B9C75CFA7316BF2098765D05', 'CDwindow-796B9EF6777039A036CCF7C3D452807A', 'CDwindow-1DF355426CF56902DC339955FF55C9AE', 'CDwindow-1B700B499C716086FA269E89B0ED3835']
for handle in driver.window_handles:
driver.switch_to.window(handle)
try:
checkbox = driver.find_element(By.XPATH,'//div[#id="checkbox"]')
print('found')
except:
pass
but I get the same error "No such element"
Talking about solving the captch : I have an external API that does it for me, but I need to click that check box here, but stuck in switching to the modal part
Note that : to reproduce issue you can use same code above, no need to create account.
Problem is that you have 2 nested iframes in the site,
You'll have to perform driver.switch_to.frame() for each of them:
# After pressing "sign-in"
captcha_iframe = driver.find_element(By.CSS_SELECTOR, '[title="Captcha"]')
driver.switch_to.frame(captcha_iframe)
inner_iframe = driver.find_element(By.CSS_SELECTOR, 'iframe')
driver.switch_to.frame(inner_iframe)
# Perform captcha check
driver.find_element(By.CSS_SELECTOR, '#checkbox').click()
Notice that after each switch_to, I directly use the new driver's context, and I do not perform a search under the element I found.
Use case:
Sending out automated WhatsApp messages using pythonanywhere. Step by step logic below:
non-coders write on a gsheet the phone numbers to which we should
send out the messages
the gsheet data is read (using gspread in pythonanywhere)
open WhatsApp URL to send out the messages in bulk
I have a code using selenium running on my machine that opens the web whatsapp url, finds the needed elements on the website, and sends the messages to the gsheets phone numbers - find below a snippet from that part of the code that I am using on my machine:
global driver
driver.get('https://web.whatsapp.com/')
waiter.until(EC.title_is("WhatsApp"))
waitCounter = 0
while 1:
try:
waiter.until(EC.presence_of_element_located((By.XPATH, "//canvas[#aria-label='Scan me!']")))
waitCounter+=1
if waitCounter%1000 == 0:
print("Waiting for user to log in...", 'WARNING')
except:
print("Logged in to WhatsApp")
break
for entry in data:
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(str(entry['PhoneNumber']))
time.sleep(2)
driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(Keys.ENTER)
time.sleep(2)
driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
time.sleep(2)
driver.find_element_by_class_name('_4sWnG').click()
time.sleep(2)
Doubt:
To make step nr 3. working on python anywhere I would have to use a headless browser. However, to initiate whatsapp web we always need to do the QR code scan, so I am not being able to do it that way. Find below the current (useless) part of my code with the headless selenium code - (NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[#id='side']/div[1]/div/label/div/div[2]"}). I am quite stuck here. Any tip or idea to overcome this is more than welcome and happy to discuss any possible solutions using other libraries that you guys might find appropriate.
Thanks in advance.
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options)
def send_whatsapp_message():
global driver
driver.get('https://web.whatsapp.com/')
print("Done updating, check the spreadsheet now")
#redirect('https://web.whatsapp.com/', code=302)
for entry in data:
driver.find_element_by_xpath("//*[#id='side']/div[1]/div/label/div/div[2]").send_keys(str(entry['PhoneNumber']))
time.sleep(2)
driver.find_element_by_xpath("//*[#id='side']/div[1]/div/label/div/div[2]").send_keys(Keys.ENTER)
time.sleep(2)
driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
time.sleep(2)
driver.find_element_by_class_name('_4sWnG').click()
time.sleep(2)
print("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')
i am trying to log in in a particular website , i fill out email and password and then i click on "login " , but actually it does not login but it redirects me to the home page, i studied the situation and noticed that some websites has a "callback" function, that you can find trought console ,in fact , when i have to register (no login, but sign up) it asks me for captcha, and trought console i found that captcha has a call back function that - if called - it simulates the " register now " button (i easly found this function trought a script that find callback linked to captcha ).
the point is : how can i find this callback function in case of login that has no captcha ? i dont know how to start, any suggestions or guide to study? thats my simply script by the way
driver.get("https://www.streetmachine.com/login")
time.sleep(1)
el=driver.find_element_by_name("email")
el.send_keys(lf[0])
el=driver.find_element_by_name("password")
el.send_keys(lf[1])
time.sleep(3)
el=driver.find_element_by_id("login-form-submit")
driver.execute_script("arguments[0].click();", el)
as you can see the website is the one called in get() , thanks !
You could try to use the "ActionChains"; find the element first and then perform the click.
For example as the following code:
def click_login_button(self, **kargs):
try:
button = WebDriverWait(self.driver, 3).until(
lambda x: x.find_element_by_id("login-form-submit")
)
except TimeoutException:
pass
if not button.is_enabled():
pass
webdriver.ActionChains(self.driver).move_to_element(button).click().perform()
Reference:
Mouse actions in detail, https://www.selenium.dev/documentation/en/support_packages/mouse_and_keyboard_actions_in_detail/
I'm curious if anyone has found a work around for handling the random "Please Verify you are human" pop up in FireFox when using Selenium and BeautifulSoup. Currently, it pops up about every 500 or 1,000 URL requests, but I'd love an automated workaround.
My driver is just the default driver = webdriver.Firefox() with selenium. The pop up is a press AND hold button (picture below) which I've just done manually as I've seen it pop up. Any info would be great thanks!
So I've figured out a workaround for this. Since the URL doesn't actually change / redirect when the 'Please verify you are human' popup occurs I've added a step prior to getting the elements with beautifulSoup.
For each URL in the list that is being scraped I do a time.sleep(5.5) to allow URL to fully load or for the verify popup to occur. Then, I interact with the URL and look for the verify indicator. For StockX it works like this: while true, try soup.find('div', class_='page-title').text and if it finds '\nPlease verify you are a human\n' then close browser and sleep (driver.quit() and time.sleep(20)) else scrape elements.
I dont have the full code written up to work but I do know I can detect if its a verify page as mentioned above. Something like this below maybe:
for url in url_list:
for attempt in range(5):
try:
if soup.find('div', class_='page-title').text == '\nPlease verify you are a human\n':
driver.quit()
time.sleep(20)
else:
scrape_everything()
except:
print(f'Hit Verify Page Attempt Num.: {attempt}')
else:
break
else:
continue