Selenium通过xpath带来错误的元素,即使路径是正确的



我一直在使用python制作一个简单的selenium脚本。

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:UsersBRSDesktopwattpad.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()

一切都很好,直到有一天我开始遇到这个问题

File "C:UsersBRSDesktopAccountChefwattpad 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:UsersBRSAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 394, in find_element_by_xpath
return self.find_element(by=By.XPATH, value=xpath)
File "C:UsersBRSAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 976, in find_element
return self.execute(Command.FIND_ELEMENT, {
File "C:UsersBRSAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdriverremotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersBRSAppDataLocalProgramsPythonPython38-32libsite-packagesseleniumwebdriverremoteerrorhandler.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!')]"}

相信我,我尝试了所有类型的选择器,但不知何故,网络驱动程序总是错过了这一点,我遇到了一个错误。我出错的按钮包含文本";这就是我&";。

您在页面上查找的按钮位于iframe内部。这意味着你不能使用硒来访问它里面的信息。据我所知,他们以前没有iframe(这就是为什么你的代码过去可以工作的原因(,但后来改为有了它(可能是为了防止刮取?(。所以要解决这个问题,你只需要查找iframe的url,打开页面,然后在那里查找按钮url。以下是代码的样子:

此行之后:

WebDriverWait(second_tab, 90).until(EC.element_to_be_clickable((By.XPATH, "//tbody[@id='schranka']/tr/td"))).click()
time.sleep(2)

你需要编辑你的代码,使按钮的链接变成这样。

#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)

相关内容

  • 没有找到相关文章