无法找到元素:硒

时间:2020-04-10 20:24:44

标签: selenium selenium-webdriver automation selenium-chromedriver nosuchelementexception

enter image description here

我想通过点击选择 更多信息 链接。我已经尽力了,但是每次错误 NoSuchElementException:没有这样的元素:无法找到元素:{“” method“:” xpath“,” selector“ 弹出。

起初,我以为是因为我没有正确更改选项卡,所以才显示此错误。但是,即使使用 window_handles 后,我仍然无法在此页面上找到任何元素。

帮助

self.driver.window_handles
        base = self.driver.window_handles[0]        
        child = self.driver.window_handles[1]

        window_set = {self.driver.window_handles[0], self.driver.window_handles[1]}

for x in window_set:
        if(base != x):
            self.driver.switch_to.window(x)
            self.driver.find_element_by_id("mc-lnk-moreInfo").click() 

2 个答案:

答案 0 :(得分:0)

尝试wait for the element,然后单击它

替换

void parse(char *userInput, char **splitInput)
{
  //read until userInput is not end of line
  while (*userInput != '\0')
  {
    //replace any space in userInput as '\0'
    while (*userInput == ';')
    {
      *userInput++ = '\0';
    }
    //save the argument position
    *splitInput++ = userInput;
    //if userinput is not equal to space, read next userInput
    while (*userInput != ' ' && *userInput != ';' && *userInput != '\0')
    {
      userInput++;
    }
  }
}

void execute(char **splitInput)
{
  pid_t pid = fork();

  if (pid > 0) //parent process
  {
    pid_t parent_pid;
    parent_pid = wait(NULL);
  }
  else if (pid == 0) //child process
  {
    if (execvp(*splitInput, splitInput) < 0) 
    {
      printf("%s: command not found\n", *splitInput);
      exit(1);
    }      
  }  
  else //error
  {
    perror("fort error");
  }
}

void main(void)
{
  char userInput[100]; //execvp's first argument
  char *splitInput[100]; //execvp's second argument

  while(strcmp(userInput,"quit") != 0)
  {
    //ask for a user input
    printf("group 10> ");
    //read the entire line of input
    scanf("%[^\n]", userInput);
    //get characters from input; stop the loop problem
    getchar();
    //quit if user input is equal to quit
    if (strcmp(userInput, "quit") == 0)
    {
      exit(0);
    }
    //parse the input
    parse(userInput, splitInput);
    //execute fork
    execute(splitInput);
  }
}

以下

self.driver.find_element_by_id("mc-lnk-moreInfo").click()

在您的导入内容上添加以下内容

  self.more_info = WebDriverWait(self.driver, 30).until(
        ec.visibility_of_element_located((By.ID, "//a[@id='mc-lnk-moreInfo']")))
    ActionChains(self.driver).move_to_element(self.more_info).click().perform()

答案 1 :(得分:0)

请使用包含和ID检查以下解决方案

包含的Xpath

 element= WebDriverWait(self.driver, 30).until(
            EC.element_to_be_clickable((By.XPATH, '//*[contains(text(), 'More information')]')))

 self.driver.execute_script("arguments[0].click();", element)

具有ID的Xpath

element= WebDriverWait(self.driver, 30).until(
        ec.element_to_be_clickable((By.ID, "//a[@id='mc-lnk-moreInfo']")))

 self.driver.execute_script("arguments[0].click();", element)

工作解决方案:

driver = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
driver.maximize_window()
wait = WebDriverWait(driver, 10)
driver.get("your url")
childframe = wait.until(EC.presence_of_element_located((By.NAME, "mainFrame")))
driver.switch_to.frame(childframe)
element=wait.until(EC.element_to_be_clickable((By.ID, "mc-lnk-moreInfo")))
print element.text
element.click()

注意::请将以下导入项添加到您的解决方案中

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

输出: enter image description here