Reputation: 273
I have been making a simple selenium script using python.
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
import string
import random
# initializing size of string
a = 1
while a==1:
N = 7
# using random.choices()
# generating random strings
res = ''.join(random.choices(string.ascii_uppercase +
string.digits, k = N))
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver=webdriver.Chrome(options=options)
driver.get("https://www.wattpad.com/login")
signup=driver.find_element_by_xpath("/html/body/div[3]/div/div[3]/div/div[2]/main/div/div/div/footer/span/button")
signup.click()
button=driver.find_element_by_xpath ("/html/body/div[3]/div/div[3]/div/div[2]/main/div/div/div/div/div/button")
button.click()
username=driver.find_element_by_id ("signup-username")
username.send_keys(str(res))
second_tab = webdriver.Chrome(options=options,)
second_tab.get("https://www.fakemail.net/")
randombutton = second_tab.find_element_by_xpath("//div[2]/div/a") #for the email generation
time.sleep(2)
emailentry = second_tab.find_element_by_xpath("/html/body/div[2]/div[2]/div[1]/div[1]/span[1]").text
print ("your email is " + emailentry)
emailspace = driver.find_element_by_id ("signup-email")
emailspace.send_keys(emailentry)
password = driver.find_element_by_id ("signup-password")
password.send_keys ("subscribe")
driver.find_element_by_xpath("//select[@id='signup-month']/option[text()='Nov']").click()
driver.find_element_by_xpath("//select[@id='signup-day']/option[text()='18']").click()
driver.find_element_by_xpath("//select[@id='signup-year']/option[text()='1996']").click()
submitbutton=driver.find_element_by_xpath("//input[@value='Sign up with email']").click()
print("acount maker done")
with open(r"C:\Users\BRS\Desktop\wattpad.txt", 'a',encoding = "utf-8") as outfile:
outfile.write(emailentry)
outfile.close()
time.sleep(10)
WebDriverWait(second_tab, 90).until(EC.element_to_be_clickable((By.XPATH, "//tbody[@id='schranka']/tr/td"))).click()
time.sleep(2)
linky= second_tab.find_element_by_xpath("//a[contains(text(),'This is me!')]").get_attribute('href')
second_tab.get(linky)
'''verilink= input("enter the verification link:\n")
driver.get(verilink)'''
time.sleep(5)
driver.get("https://www.wattpad.com/893851831-my-2nd-year-chapter-1")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/893856960-my-2nd-year-chapter-2-10-minutes-later")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/894447654-my-2nd-year-chapter-3")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/895034288-my-2nd-year-chapter-4-6-hours-later")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/896648779-my-2nd-year-chapter-5-after-1-week")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/897330154-my-2nd-year-specials-chapter-6")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/898580167-my-2nd-year-chapter-7")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/899494005-my-2nd-year-chapter-8")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/901463787-my-2nd-year-chapter-9")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/902498234-my-2nd-year-after-an-hour-chapter-10")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
driver.get("https://www.wattpad.com/918598116-my-2nd-year-chapter-11")
vote1=driver.find_element_by_xpath("/html/body/div[4]/div/div[1]/div/div[2]/div[2]/button/span[2]")
vote1.click()
print("Done added 11 votes")
time.sleep(5)
driver.quit()
second_tab.quit()
everything was working fine untill one random day i started getting this problem
File "C:\Users\BRS\Desktop\AccountChef\wattpad acc maker - Copy.py", line 66, in <module>
linky= second_tab.find_element_by_xpath("//a[contains(text(),'This is me!')]").get_attribute('href')
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Users\BRS\AppData\Local\Programs\Python\Python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[contains(text(),'This is me!')]"}
trust me i tried every type of selector but somehow this is always missed by the webdriver and i encounter an error. the button i get error on contains text "This is me!".
Upvotes: 0
Views: 1024
Reputation: 442
The button that you are looking for on the page is inside of an iframe. Which means you can not access the information inside of it using selenium. Its my understanding that they previously didn't have an iframe (thats why your code used to work) but then changed to have it (probably to prevent scraping?). So to solve this all you need to do is look for the url of the iframe, open the page, and just look for the button url there. Here is what the code looks like:
After this line:
WebDriverWait(second_tab, 90).until(EC.element_to_be_clickable((By.XPATH, "//tbody[@id='schranka']/tr/td"))).click()
time.sleep(2)
You need to edit your code that gets the link of the button to be like this.
#get iframe url
src = second_tab.find_element_by_xpath('/html/body/div/div[3]/div/div[2]/div/div/iframe').get_attribute('src')
#open iframe url
second_tab.get(src)
#get button
linky = second_tab.find_element_by_xpath("//a[contains(text(),'This is me!')]").get_attribute('href')
#open button link
second_tab.get(linky)
Upvotes: 1