小编Irw*_*win的帖子

如何让Selenium点击可变数量的"下一个"按钮?

我有一个内部Web应用程序,它有一个模态对话框.不幸的是,我不能在这里发布实际的Web应用程序位置,但让我尽可能地描述它.

  • 当应用程序启动时,屏幕上会出现一个框,告诉您一堆文本.您可以按"下一步"以获取下一页文本.
  • 在最后一页上,"下一步"按钮被禁用,并且启用了Web应用程序的其余UI.
  • 页面数量可变,所以我不知道有多少次点击"下一步".

我可以点击固定次数(例如:如果我知道有两个页面我可以点击两次)但我不确定如何改变它以便无论我有多少页面它都会运行.我想要一个普遍的解决方案; 据推测,这会使用某种循环来检查按钮是否已启用.如果是,则单击它.如果它被禁用,则退出循环.

问题是:如何在Selenium中设置循环,重复单击按钮直到它被禁用?

这是我尝试过的代码:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

driver.get("http://localhost/myapp")

try:
    wait = WebDriverWait(driver, 100)    
    wait.until(EC.element_to_be_clickable((By.ID,'menuIntroBox_buttonNext')))    
    driver.find_element_by_id("menuIntroBox_buttonNext").click()

    # Click through the introduction text... this is the problematic code.
    # Here I tried to wait for the element to …
Run Code Online (Sandbox Code Playgroud)

python selenium selenium-webdriver

8
推荐指数
1
解决办法
5327
查看次数

你可以使用Python正则表达式从偏移量向后搜索吗?

给定一个字符串,以及该字符串中的字符偏移量,我可以使用Python正则表达式向后搜索吗?

我试图解决的实际问题是在字符串中的特定偏移处获得匹配的短语,但我必须匹配该偏移之前的第一个实例.

在我有一个长度为一个符号的正则表达式的情况下(例如:单词边界),我正在使用一个解决方法来反转字符串.

my_string = "Thanks for looking at my question, StackOverflow."
offset = 30
boundary = re.compile(r'\b')
end = boundary.search(my_string, offset)
end_boundary = end.start()
end_boundary
Run Code Online (Sandbox Code Playgroud)

产量:33

end = boundary.search(my_string[::-1], len(my_string) - offset - 1)
start_boundary = len(my_string) - end.start()
start_boundary
Run Code Online (Sandbox Code Playgroud)

产量:25

my_string[start_boundary:end_boundary]
Run Code Online (Sandbox Code Playgroud)

输出:'问题'

但是,如果我有一个可能涉及多个字符的更复杂的正则表达式,这种"反向"技术将无法工作.例如,如果我想匹配在指定偏移量之前出现的第一个"ing"实例:

my_new_string = "Looking feeding dancing prancing"
offset = 16 # on the word dancing
m = re.match(r'(.*?ing)', my_new_string) # Except looking backwards
Run Code Online (Sandbox Code Playgroud)

理想输出:喂食

我可以使用其他方法(将文件拆分为行,并向后遍历行)但向后使用正则表达式似乎是一个概念上更简单的解决方案.

python regex python-2.7

8
推荐指数
1
解决办法
3894
查看次数

当我尝试使用混合数据类型融合数据框时,我会获得NAs.我该如何最好地解决这个问题?

我的目标和背景

我在R中有一个数据框,我想使用reshape2库进行融合.有两个原因.

  1. 我想使用ggplot绘制条形图中每个问题的每个用户的分数.

  2. 我想把这些数据放到Excel中,这样我就可以看到,每个用户,他们的情绪,分数,以及混合的动机,态度,等等.我的意图是使用融合,然后转换为将数据放入宽格式以便于Excel导入.

我的问题

当我尝试运行熔化时,我收到一个警告,最终在我生成​​的熔融数据框中出现了NA.

Warning messages:
1: In `[<-.factor`(`*tmp*`, ri, value = c(0.148024, 0.244452, -0.00421,  :
invalid factor level, NAs generated
2: In `[<-.factor`(`*tmp*`, ri, value = c(0L, 0L, 0L, 0L, 0L, 0L, 0L,  :
invalid factor level, NAs generated
Run Code Online (Sandbox Code Playgroud)

我最终在我的融合数据框中输入了大量的NA.我认为这是因为我在同一列中同时使用了字符和数字.

我的问题

结果我有两个问题.

问题1:R中是否有解决方法?

问题2:我是否有更好的方法来构建数据以避免此问题?

码

这是我创建数据框的代码.

words <- data.frame(read.delim("sentiments-test-subset-no-text.txt", header=FALSE))
names(words) <- c("level", "question", "user", "sentiment", "score", "mixed")
words$user <- as.factor(words$user)
words.m <- melt(words, id.vars=c("user", "level"), measure.vars=c("sentiment", "score",     "mixed"))
Run Code Online (Sandbox Code Playgroud)

我很难重塑和融化,但我认为这就是我想要的最后一行.

数据

人类可读格式的数据如下所示.

experimental    motivated   1 …
Run Code Online (Sandbox Code Playgroud)

r reshape

5
推荐指数
1
解决办法
3653
查看次数

标签 统计

python ×2

python-2.7 ×1

r ×1

regex ×1

reshape ×1

selenium ×1

selenium-webdriver ×1