Unclickable button in selenium - python

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
chrome_options = webdriver.ChromeOptions()
driver = webdriver.Chrome(r'chromedriver.exe', options=chrome_options)
url = 'https://rarible.com/connect'
b = 1
def main():
driver.get(url)
input('Login..(Press ENTER when finsihed)')
sleep(1)
while driver.current_url == "https://rarible.com/":
ts_href = '/html/body/div[1]/div[2]/div[2]/div[2]/div/div/div/div[4]/div[2]/div/div[1]/div[1]/div/div/div[' \
'1]/div[1]/div/div[3]/a'
href = driver.find_element(By.XPATH, ts_href).get_attribute('href')
driver.get(href)
sleep(2)
followers = '/html/body/div/div[2]/div[2]/div[2]/div[2]/div/div/div/div[1]/div[5]/div/button[1]/span[2]'
driver.find_element(By.XPATH, followers).click()
sleep(3)
# buttons = driver.find_elements(By.XPATH, '//button[normalize-space()="Follow"]')
def butts():
global b
fbtn = f'/html/body/div/div[1]/div/div[2]/div/div[2]/div/div/div/div[{b}]/div/div/div[3]/button'
buttons = driver.find_element(By.XPATH, fbtn)
print(f'BUTTON {b} TEXT (FOLLOW/UNFOLLOW): {buttons.text}')
if buttons.text == "Follow":
buttons.click()
b += 1
sleep(1)
butts()
elif buttons.text == "Unfollow":
b += 1
butts()
butts()
print('All set here, onto the next one...')
else:
driver.get('https://rarible.com/')
if __name__ == '__main__':
main()
I cannot get it to click the follow buttons.
I Can't find an iframe that they are hiding in, or any other type of javascript voodoo being done but i am not the most experienced. Which is why i come to you seeking guidance.

The line where you define followers wasn't working for me so I changed it to
followers = '//button[#datamarker="root/appPage/address/profile/followingLinks/followers"]'
Trying to figure out how to get the butts() loop to run, but I don't have any followers on rarible so it's kind of hard to test. However, putting these lines of code in at the end of "if buttons.text == "Follow": before butts() is called again might work.
scroll_div = driver.find_element(By.XPATH, '/html/body/div/div[1]/div/div[2]/div/div[2]/div/div/div')
scroll_div.send_keys(Keys.ARROW_DOWN)
It's not actually an iframe. The solution has something to do with scrolling through the follow buttons

Related

selenium click() method doesn't work after perfectly working a couple of hours before

I'm using selenium to like automatically some pictures. So the script searches for a word, clicks on the first picture, locates the like button then clicks on it.
The click button works to click on the picture but not to like it... I didn't change the identifier, so the class name is perfectly fine. I don't where the problem could come.
Here's my code :
'''
class Interact(Browser):
#click on the firt picture in the page
def firstpic_click(self):
time.sleep(2)
first = self.browser.find_element_by_class_name("kIKUG")
first.click()
#likes curent picture
def like_pic(self):
time.sleep(2)
like = self.browser.find_element_by_class_name("fr66n")
soup = bs(like.get_attribute('innerHTML'),'html.parser')
if (soup.find('svg')['aria-label'] == 'Like'):
like.click()
time.sleep(2)
#returns and clicks on next picture if any
def next_picture(self):
time.sleep(2)
try:
# nex = browser.find_element_by_xpath('//svg[#aria-label="Next"]')
next = self.browser.find_elements_by_class_name("wpO6b ")
soups = [bs(nextt.get_attribute('innerHTML'),'html.parser') for nextt in next]
for i in range(len(soups)):
#print(soups[i])
if (soups[i].find('svg')['aria-label'] == 'Next'):
next[i].click()
return next[i]
#nex = self.browser.find_element_by_xpath('//button[#class="wpO6b "]')
#time.sleep(1)
#return nex
except selenium.common.exceptions.NoSuchElementException:
return 0
#liking all the next pictures if any
def continue_liking(self):
while(True):
next_el = self.next_picture()
if next_el != False:
self.like_pic()
time.sleep(2)
next_el.click()
time.sleep(2)
else:
print("not found")
break
def word_search(self, search=1):
# word = input("what?")
word = "lol"
#search is the method of search
#looking for word in search box
if search == 0:
search_box = self.browser.find_element_by_xpath("//input[#aria-label='Search Input']")
search_box.send_keys("#"+word)
time.sleep(2)
#search_box.send_keys(Keys.RETURN)
#search_box.send_keys(Keys.RETURN)
search_box.submit()
time.sleep(5)
#type the website directly
if search == 1:
self.browser.get("https://www.instagram.com/explore/tags/" + word)
def liking_pictures(browser0):
browser0.implicitly_wait(5)
browser = Interact(browser0)
browser.word_search()
browser.firstpic_click()
browser.like_pic()
browser.next_picture()
browser.continue_liking()
time.sleep(10)
if __name__ == '__main__':
with browse() as browser0:
#unsubscribing(browser0)
liking_pictures(browser0)
'''
Thank you

selenium python script is malfunctioning (Python Omagle Bot Script)

i am programming a simple omagle bot to learn more about the selenium.
what my scrips should be doing?
1.open the Omegle site, write some interest and press on the text button [this part is working]
2.it should type the text I defined it to text and then click on Esc button and then repeat this thing properly [this part is partially working]
Below is my script.
import random
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
# Settings
tags = ['test']
browser = webdriver.Chrome()
print(">>Opening Browser window : ")
browser.get("https://www.omegle.com/")
print(">>Opening Site : ")
time.sleep(random.randint(2, 4))
print(">>loading page : ")
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
text_area = browser.find_element_by_class_name("topicplaceholder")
text_area.click()
input = browser.find_element_by_class_name("newtopicinput")
for i in tags:
input.send_keys(i)
print(">>Tag Entered > " + i)
input.send_keys(Keys.RETURN)
time.sleep(random.randint(1, 3))
browser.find_element_by_xpath("//img[#id='textbtn']").click()
print("opening a new chat : ")
i = 0
while i <= 50:
try:
time.sleep(2)
browser.find_element_by_xpath("/html/body/div[6]/div/div/div[1]/div[1]/div/div[4]/div/div/img").click()
print("clicked Next chat button")
except:
try:
time.sleep(10)
time.sleep(3)
browser.find_element_by_xpath("//button[#class='disconnectbtn']").click()
print("ESCAPE pressed ")
print("waiting for 10 sec")
# Here messages add as manny as you want
browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/table/tbody/tr/td[2]/div/textarea").send_keys(
"Hey")
print("hy sent")
browser.find_element_by_xpath("//button[#class='sendbtn']").click()
time.sleep(5)
browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/table/tbody/tr/td[2]/div/textarea").send_keys(
"How are you?")
print("how are u sent")
browser.find_element_by_xpath("//button[#class='sendbtn']").click()
time.sleep(10)
browser.find_element_by_xpath("/html/body/div[5]/div/div/div[2]/table/tbody/tr/td[2]/div/textarea").send_keys(
"i am steve.")
print("i am steve")
browser.find_element_by_xpath("//button[#class='sendbtn']").click()
time.sleep(5)
i += 1
print(">Number Of messages send : ", str(i))
except:
print("pass")
try:
browser.find_element_by_xpath("/html/body/div[5]/div/div/div[1]/div[1]/div/div[6]/div/div/img").click()
print("clicked Next chat button")
except:
browser.find_element_by_xpath("//button[#class='disconnectbtn']").click()
print("ESCAPE pressed ")
browser.find_element_by_xpath("//button[#class='disconnectbtn']").click()
print("ESCAPE pressed ")

Going through paginated table with Selenium in Python

I am going through the table at the following link:
http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881
through selenium in python. This is the code:
driver = webdriver.Chrome()
driver.get('http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881')
elem = driver.find_element_by_link_text("Variants")
while elem:
elem.click()
time.sleep(5)
try:
elem = driver.find_element_by_link_text("Next")
print(elem.is_enabled())
if 'disabled' in elem.get_attribute('class'):
break
except:
print('Next not available or page not loaded!')
driver.quit()
I have trouble changing the number of displayed values to 100. How would I do that?
Also, why does the is_enabled() return True even when the button becomes unclickable?
Thanks in advance!
Q: I have trouble changing the number of displayed values to 100. How would I do that?
Solution: You have to use Select Class (link) to select value from Dropdown
Question:why does the is_enabled() return True even when the button becomes unclickable?
The is_enabled() is returning True only when it is as you can see the True is only printed three times when it was enabled and post that its enter in break loop and exits as expected
Just updated your code with Select and break print statement:
from selenium import webdriver
import time
from selenium.webdriver.support.ui import Select
driver = webdriver.Chrome()
driver.maximize_window()
driver.get('http://cancer.sanger.ac.uk/cosmic/sample/overview?id=2120881')
elem = driver.find_element_by_link_text("Variants")
while elem:
elem.click()
time.sleep(5)
select = Select(driver.find_element_by_name('DataTables_Table_0_length'))
select.select_by_value('100') # you can use any value
try:
elem = driver.find_element_by_link_text("Next")
print(elem.is_enabled())
if 'disabled' in elem.get_attribute('class'):
print "Before Break"
break
except:
print('Next not available or page not loaded!')
driver.quit()
The output is:
True
True
True
Before Break

how to wait for a specific command to finish in selenium

I'm trying to get some info from pages that look like this link to the site
I need to scroll all the way down, I wrote a function for that.
def Scroll():
startPos = driver.execute_script("return window.pageYOffset;")
while 1:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") #need to wait
time.sleep(0.1)
newPos = driver.execute_script("return window.pageYOffset;")
if newPos == startPos:
break
startPos = newPos
return
I would like to change time.sleep(0.1) with some conditional wait until the line before that one is executed, is there a way to do this? There is no special element loaded after every scroll or at the end of the page.
Solution:
...
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
while 1:
if driver.execute_script("return jQuery.active == 0"):
break
else:
time.sleep(0.05)
continue
...
Instead of thread.sleep, you can use following code which waits for Ajax calls to complete because every scroll, there will be a Ajax call in the background.
WebDriverWait wait=new WebDriverWait(driver, 120);
Wait.until(ExpectedConditions.jsReturnValue("return jQuery.active == 0"));

How to make a super method to cover four base methods with python?

The code below is written by me, however I find the method is not suitable sometimes. I don't know how to make a good super method to cover four base selenium web-driver methods.
def keymouse(url, operation , elementxpath):
driver = webdriver.Chrome()
driver.get(url)
time.sleep(1)
driver.maximize_window()
time.sleep(1)
operation_by = operation.split('.')[1]
if operation_by == "context_click()":
result = ActionChains(driver).context_click(driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == "double_click()":
result = ActionChains(driver).double_click(driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == "drag_and_drop()":
result = ActionChains(driver).drag_and_drop(driver.find_element_by_xpath(elementxpath), driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == "click_and_hold()":
result = ActionChains(driver).click_and_hold(driver.find_element_by_xpath(elementxpath)).perform()
else:
time.sleep(3)
This is the instance to use the above method:
from method.key_mouse import *
#driver = webdriver.Chrome()
keymouse("https://www.baidu.com", "operation.context_click()", "//*[#id='kw']")
A good way would be to use the enum module.
from enum import Enum
# Create your desired operations
class KeyMouseOperation(Enum):
CONTEXT_CLICK = 1
DOUBLE_CLICK = 2
DRAG_AND_DROP = 3
CLICK_AND_HOLD = 4
from method.key_mouse import *
#driver = webdriver.Chrome()
keymouse("https://www.baidu.com", KeyMouseOperation.CONTEXT_CLICK, "//*[#id='kw']")
def keymouse(url, operation , elementxpath):
driver = webdriver.Chrome()
driver.get(url)
time.sleep(1)
driver.maximize_window()
time.sleep(1)
if operation_by == KeyMouseOperation.CONTEXT_CLICK:
result = ActionChains(driver).context_click(driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == KeyMouseOperation.DOUBLE_CLICK:
result = ActionChains(driver).double_click(driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == KeyMouseOperation.DRAG_AND_DROP:
result = ActionChains(driver).drag_and_drop(driver.find_element_by_xpath(elementxpath), driver.find_element_by_xpath(elementxpath)).perform()
if operation_by == KeyMouseOperation.CLICK_AND_HOLD:
result = ActionChains(driver).click_and_hold(driver.find_element_by_xpath(elementxpath)).perform()
else:
time.sleep(3)

Categories

Resources